首先,我對PHP非常陌生,所以我仍在嘗試。我的問題是:我試圖在PHP中迴應Bing API結果。顯示json結果的PHP問題
這裏是一個JSON結果例子,這是我從
$jsonobj = json_decode($response);
得到(的反應是我從兵得到的,所以我只粘貼了以下回應 - 萬一添加此信息你不知道在哪裏得到$jsonobj = json_decode($response);
從)
{"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":[]}]}}
現在,我明白,我可以重複,例如,WebTotal使用:
foreach($jsonobj->d->results as $value) {
echo $value->WebTotal;
}
但是,我迷失在如何迴應實際結果,如Title
,Description
和Url
。
我想:
foreach($jsonobj->d->results as $value) {
echo $value->Title."<br>";
echo $value->Description."<br>";
echo $value->Url."<br>";
}
也類似:
foreach($jsonobj->d->results->Web as $value) {
echo $value->Title."<br>";
echo $value->Description."<br>";
echo $value->Url."<br>";
}
,因爲我認爲添加Web到foreach
將可能呼應正確的價值觀,但沒有成功。
也許有人可以幫助我,告訴我我做錯了什麼?
我的任務將有如下結果:
標題:PHP:Hypertext Preprocessor的
描述:PHP是什麼? PHP是一種廣泛使用的通用 腳本語言,特別適合Web開發,並且 可以嵌入到HTML中。
的URL:http://www.php.net/
然後,其他兩個結果。
太謝謝你了:)
有時,它幫助打印出對象的格式化版本,以更好地瞭解這些元素。 :)'printf('
',print_r($ value,1));' – Sean3z