为 foreach() 提供的参数无效 - Bing Search API [英] Invalid argument supplied for foreach() - Bing Search API

查看:22
本文介绍了为 foreach() 提供的参数无效 - Bing Search API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是php代码:

<?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;

?>

这是 html:

<html>
<head>
<title>Bing Search Tester (Basic)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Bing Search Tester (Basic)</h1>
<form method="POST" action="bing_basic.php">
<label for="service_op">Service Operation</label><br/>
<input name="service_op" type="radio" value="Web" CHECKED /> Web
<input name="service_op" type="radio" value="Image" /> Image <br/>
<label for="query">Query</label><br/>
<input name="query" type="text" size="60" maxlength="60" value="" /><br /><br />
<input name="bt_search" type="submit" value="Search" />
</form> <h2>Results</h2>
{RESULTS} 
</body>
</html>

为什么我总是收到那个错误?顺便说一下,这是重复的,但其他问题没有所有代码,我的问题也没有答案.

Why do I keep getting that error ? This is a duplicate by the way, but the other questions didn't have all the code and there were no answers to my question.

另外,我对 php 对象和 json 不是很熟悉..

Also, I'm not very familiar with php objects and json..

API 文档:https://onedrive.live.com/view.aspx?resid=9C9479871FBFA822!112&app=Word&authkey=!ANNnJQREB0kDC04

推荐答案

仔细检查以确保 $jsonObj->d->results 确实是一个数组或它不为空.

Double check to ensure that $jsonObj->d->results is indeed an array or that its not empty.

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!";
    }
}

这篇关于为 foreach() 提供的参数无效 - Bing Search API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆