我在博客中使用了图片搜索Bing API。我有一个请求给我这个回复:
stdClass Object
(
[d] => stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='Kitchen'&Market='en-us'&$skip=0&$top=1
[type] => ImageResult
)
[ID] => a40b8c85-8a6b-45a8-bce2-c07b16a942e6
[Title] => Our Kitchen Remodel is Complete!!! @ A Well Dressed Home
[MediaUrl] => http://awelldressedhome.com/wp-content/uploads/2010/10/Kitchen-31.jpg
[SourceUrl] => http://awelldressedhome.com/496-our-kitchen-remodel-is-complete/
[DisplayUrl] => awelldressedhome.com/496-our-kitchen-remodel-is-complete
[Width] => 4000
[Height] => 3000
[FileSize] => 5062458
[ContentType] => image/jpeg
[Thumbnail] => stdClass Object
(
[__metadata] => stdClass Object
(
[type] => Bing.Thumbnail
)
[MediaUrl] => http://ts4.mm.bing.net/th?id=OIP.Mf5b92bb26c7b2ed74a31df5d5836cb70H0&pid=15.1
[ContentType] => image/jpg
[Width] => 480
[Height] => 360
[FileSize] => 25427
)
)
当我试图得到像这样的[MediaUrl]时:
$key = "cricket";
$accountKey = 'iXX2NrEp8gfTPvsahjaj2KUAT+E7Quwelff4B6+MDnE';
$ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';
$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';
$request = $WebSearchURL . urlencode( '\'' . $key . '\'');
$process = curl_init($request);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $accountKey . ":" . $accountKey);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($process);
$jsonobj = json_decode($response);
echo('<ul ID="resultList">');
foreach($jsonobj->Results as $value)
{
echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');
echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
}
echo("</ul>");
我收到错误:
尝试获取非对象的属性
所以问题是:我做错了什么?如何从此响应中获取[MediaUrl]?对不起我的英文,谢谢你的回复。
答案 0 :(得分:0)
请注意,缺少“d”以及区分大小写的问题,请替换此:
$jsonobj->Results
有了这个:
$jsonobj->d->results