这是php代码:
time = Time.now
time.strftime('%Y-%m-%d %I:%M:%S %p') #This gives 2015-07-15 08:28:38 PM
这是html:
<?php
$acctKey = 'key';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
$contents = file_get_contents('bing_basic.html');
if ($_POST['query'])
{
$query = urlencode("'{$_POST['query']}'");
$serviceOp = $_POST['service_op'];
$requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
$auth = base64_encode("$acctKey:$acctKey");
$data = array('http' => array('request_fulluri' => true,'ignore_errors' => true,'header' => "Authorization: Basic $auth"));
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$jsonObj = json_decode($response);
$resultStr = '';
foreach($jsonObj->d->results as $value)
{
switch ($value->__metadata->type)
{
case 'WebResult':
$resultStr .= "<a href=\"{$value->Url}\">{$value->Title}</a><p>{$value->Description}</p>";
break;
case 'ImageResult': $resultStr .= "<h4>{$value->Title} ({$value->Width}x{$value->Height}) " . "{$value->FileSize} bytes)</h4>" . "<a href=\"{$value->MediaUrl}\">" . "<img src=\"{$value->Thumbnail->MediaUrl}\"></a><br />";
break;
}
}
$contents = str_replace('{RESULTS}', $resultStr, $contents);
}
echo $contents;
?>
为什么我一直收到这个错误? 顺便说一句,这是重复的,但其他问题并没有包含所有代码,而且我的问题没有答案。
另外,我对php对象和json不是很熟悉..
Api文档:https://onedrive.live.com/view.aspx?resid=9C9479871FBFA822!112&app=Word&authkey=!ANNnJQREB0kDC04
答案 0 :(得分:2)
仔细检查以确保$ jsonObj-&gt; d-&gt;结果确实是一个数组或它不为空。
if( ( is_array( $jsonObj->d->results ) && ( ! empty( $jsonObj->d->results ) ) {
foreach($jsonObj->d->results as $value)
{
switch ($value->__metadata->type)
{
case 'WebResult':
$resultStr .= "<a href=\"{$value->Url}\">{$value->Title}</a><p>{$value->Description}</p>";
break;
case 'ImageResult': $resultStr .= "<h4>{$value->Title} ({$value->Width}x{$value->Height}) " . "{$value->FileSize} bytes)</h4>" . "<a href=\"{$value->MediaUrl}\">" . "<img src=\"{$value->Thumbnail->MediaUrl}\"></a><br />";
break;
}
}
} else {
if( ! is_array( $jsonObj->d->results ) {
echo "jsonObj->d->results is not an array!";
} elseif( empty( $jsonObj->d->results ) {
echo "jsonObj->d->results is empty!";
}
}