First of all, I am extremely new to PHP, so I am still trying. My issue is: I am trying to echo Bing API results in PHP.
Here is a JSON result example, which I get from
$jsonobj = json_decode($response);
(The response is what I get from Bing, so I just pasted the response below - Just adding this info in case you wonder where I get the $jsonobj = json_decode($response); from)
{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}
Now, I understand that I can echo, for example, the WebTotal by using:
foreach($jsonobj->d->results as $value) {
echo $value->WebTotal;
}
However, I am lost on how to echo the actual results like the Title, Description and Url.
I tried:
foreach($jsonobj->d->results as $value) {
echo $value->Title."<br>";
echo $value->Description."<br>";
echo $value->Url."<br>";
}
and also something like:
foreach($jsonobj->d->results->Web as $value) {
echo $value->Title."<br>";
echo $value->Description."<br>";
echo $value->Url."<br>";
}
because I thought adding the Web to the foreach will maybe echo the right values, but no success.
Maybe someone could help me and tell me what I am doing wrong?
My mission would be to have the following results:
Title: PHP: Hypertext Preprocessor
Description: What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
URL:http://www.php.net/
Then, the other two results.
Thank you so much :)
printf('<pre>%s</pre>', print_r($value, 1));