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

问题描述

这是PHP code:

This is the php code:

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

?>

和TIS是HTML:

And tis is the 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>

为什么我不断收到这个错误?
这是方式的重复,但其他的问题并没有全部code和有我的问题没有答案。

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文档:<一href=\"https://onedrive.live.com/view.aspx?resid=9C9479871FBFA822!112&app=Word&authkey=!ANNnJQREB0kDC04\" rel=\"nofollow\">https://onedrive.live.com/view.aspx?resid=9C9479871FBFA822!112&app=Word&authkey=!ANNnJQREB0kDC04

推荐答案

仔细检查,以确保$ jsonObj-> D->的结果确实是一个数组或任何其不为空。

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

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

08-26 11:32