Skip to content

Commit

Permalink
Fixed an issue with the get() method. Updated the isJson() method to …
Browse files Browse the repository at this point in the history
…check PHP version.
  • Loading branch information
dbough committed Jun 30, 2013
1 parent 94735e8 commit f659215
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 19 additions & 8 deletions MaasApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getArchiveSearch($params)
$jsonData = file_get_contents($this->archiveUrl . $urlSuffix);
$data = json_decode($jsonData);

return $this->get($data);
return $this->get($data, substr($urlSuffix, 1));

}

Expand All @@ -138,12 +138,14 @@ public function getArchiveSearch($params)
* @param object $data
* @return array
*/
public function get($data)
public function get($data, $urlSuffix=NULL)
{
$results = $data->results;

// Determine how many pages of data there are
$pages = ceil($data->count / 10);
if ($data->count > 10) {
$pages = ceil($data->count / 10);
}

// Context for ignoring HTTP / PHP errors with file_get_contents
$context = stream_context_create(array(
Expand All @@ -152,8 +154,8 @@ public function get($data)

// Start at page 2 and go to the end
for ($i=2;$i<=$pages;$i++) {
$jsonData = file_get_contents($this->archiveUrl . "?page=" . $i, false, $context);
$jsonData = file_get_contents($this->archiveUrl . "?page=" . $i . "&" . $urlSuffix, false, $context);

// If $jsonData is an object add it's data to our results array.
if ($this->isJson($jsonData)) {
foreach (json_decode($jsonData)->results as $result) {
Expand All @@ -165,14 +167,23 @@ public function get($data)
return $results;
}


/**
* Determines whether or not a string is a JSON object
* Since this requires PHP 5.3, I'm assuming the JSON is OK
* for any version below that. Probably a better way of doing this
* but I'll worry about that later.
* @param string $string
* @return boolean
*/
private function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
$version = substr(phpVersion(), 0, 3);

if ($version >= "5.3") {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
else {
return true;
}
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ MaasApi is a PHP class used for interfacing with the {MAAS} API.
More info on the {MAAS} API can be found at
http://marsweather.ingenology.com/ and https://github.com/ingenology/mars_weather_api

Visit http://www.danielbough.com/maas to see a real world example.

Author
------
Dan Bough
Expand All @@ -12,10 +14,10 @@ http://www.danielbough.com

License
-------
This software is covered under GPLv3.
This software is free to use under the GPLv3 license.

Example Use
-----------
-----------
*Get latest data as an array:*

$maas = new MassApi;
Expand Down

0 comments on commit f659215

Please sign in to comment.