With an API Request a get a Server response in JSOn like this in one String:
{"resultsTotal":3,"resultsPage":-1,"resultsPageSize":-1,"results":[{"id":1998425622,"name":"Regionale Mentions_Branche","type":"search string","creationDate":"2015-08-21T15:13:58.226+0000","lastModificationDate":"2015-08-21T15:13:58.226+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null},{"id":1998422533,"name":"HTP_Sponsoring","type":"search string","creationDate":"2015-08-18T08:53:38.136+0000","lastModificationDate":"2015-08-18T08:53:38.136+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null},{"id":1998422529,"name":"HTP_Brand Mentions","type":"search string","creationDate":"2015-08-18T08:41:32.699+0000","lastModificationDate":"2015-08-18T14:42:19.977+0000","lastModifiedUsername":"mueller@cybay.de","lockedQuery":false,"lockedByUsername":null}]}
so I use the json_decode to get an array.
now i want to parse the array because i only need the "id":xxxxxxxx and the "name" my code is:
$webservice = 'http://newapi.brandwatch.com/projects/';
$kundenId = $_POST["kunden"];
$key = "?access_token=XXXXXXXXX";
$onlySdate = $_POST["startdate"];
$onlyEdate = $_POST["enddate"];
$startdate = "&startDate=".$onlySdate ;
$enddate = "?endDate=" .$onlyEdate ;
$url = $webservice . $kundenId . "/queries/summary".$key;
$domainRequest = $url;
//header("Location:$domainRequest");
$data = array();
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded
" .
'Authorization: Basic ' . BASIC_AUTH,
'method' => 'GET',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($domainRequest, false, $context);
$array = json_decode($result, true);
//echo count ($result);
//echo "<br>";
print_r ($array);
if i want to receive just one entry of the array i get no response or without the true in json_decode ($result);
i'll get a Fatal error: Cannot use object of type stdClass as array in /data/kunden/cylab/BH/produktion/web/htdocs_final/brandwatch/brandwatch.php on line 31.
What can i do? to see just one entry of the array and how can i parse it? thanks for help!
If you are really getting that json code from the request and you decode it with
$array = json_decode($result, true);
you will get an (associative) array that has the same structure as the json string (without the true
value, you will get objects, that's why you got the Fatal Error). In that case, you can just access its fields, like so:
foreach ($array["results"] as $result) {
echo "id=" . $result["id"] . ", name=" . $result["name"] . "
";
}
yielding the output:
id=1998425622, name=Regionale Mentions_Branche
id=1998422533, name=HTP_Sponsoring
id=1998422529, name=HTP_Brand Mentions
the response of echo '<pre>'.print_r($array, true).'</pre>';
stdClass Object
(
[resultsTotal] => 3
[resultsPage] => -1
[resultsPageSize] => -1
[results] => Array
(
[0] => stdClass Object
(
[id] => 1998425622
[name] => Regionale Mentions_Branche
[type] => search string
[creationDate] => 2015-08-21T15:13:58.226+0000
[lastModificationDate] => 2015-08-21T15:13:58.226+0000
[lastModifiedUsername] => mueller@cybay.de
[lockedQuery] =>
[lockedByUsername] =>
)
[1] => stdClass Object
(
[id] => 1998422533
[name] => HTP_Sponsoring
[type] => search string
[creationDate] => 2015-08-18T08:53:38.136+0000
[lastModificationDate] => 2015-08-18T08:53:38.136+0000
[lastModifiedUsername] => mueller@cybay.de
[lockedQuery] =>
[lockedByUsername] =>
)
[2] => stdClass Object
(
[id] => 1998422529
[name] => HTP_Brand Mentions
[type] => search string
[creationDate] => 2015-08-18T08:41:32.699+0000
[lastModificationDate] => 2015-08-18T14:42:19.977+0000
[lastModifiedUsername] => mueller@cybay.de
[lockedQuery] =>
[lockedByUsername] =>
)
)
)