微软为新的必应API提供的PHP示例不起作用。我试过很多种方法,它只显示了:
服务器错误 401 -未经授权:访问因凭据无效而被拒绝。 您没有使用您提供的凭据查看此目录或页的权限。
正式文档中给出的示例代码如下所示,它在
'proxy' => 'tcp://127.0.0.1:8888', 我100%确信我的键是正确的,当我在浏览器url中输入它时,它工作得很好,即
https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27love+message%27(您需要放置API密钥,因为您的密码和用户名可以是任何内容)
<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<title>PHP Bing</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Type in a search:
<input type="text" id="searchText" name="searchText"
value="<?php
if (isset($_POST['searchText']))
{
echo($_POST['searchText']);
}
else
{
echo('sushi');
}
?>"
/>
<input type="submit" value="Search!" name="submit" id="searchButton" />
<?php
if (isset($_POST['submit']))
{
// Replace this value with your account key
$accountKey = 'BKqC2hIKr8foem2E1qiRvB5ttBQJK8objH8kZE/WJVs=';
$ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';
$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';
$context = stream_context_create(array(
'http' => array(
//'proxy' => 'tcp://127.0.0.1:8888',
'request_fulluri' => true,
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
)
));
$request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');
echo($request);
$response = file_get_contents($request, 0, $context);
print_r($response);
$jsonobj = json_decode($response);
echo('<ul ID="resultList">');
foreach($jsonobj->d->results as $value)
{
echo('<li class="resultlistitem"><a href="' . $value->MediaURL . '">');
echo('<img src="' . $value->Thumbnail->MediaUrl. '"></li>');
}
echo("</ul>");
}
?>
</form>
</body>
</html>我试过google和Yahoo,这些都没有这么难。
发布于 2012-07-18 02:52:09
经过几天与微软技术支持部门的争论,他们认为它不起作用
这里的是在必应API中使用CURL的适当代码,应用CURL方法代替不能将正确的身份验证信息从Linux客户端传递给BING服务的file_get_contents。
<html>
<head>
<title>PHP Bing</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Type in a search:
<input type="text" id="searchText" name="searchText"
value="<?php
if (isset($_POST['searchText']))
{
echo($_POST['searchText']);
}
else
{
echo('sushi');
}
?>"
/>
<input type="submit" value="Search!" name="submit" id="searchButton" />
<?php
if (isset($_POST['submit']))
{
$credentials = "username:xxx";
$url= "https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27{keyword}%27";
$url=str_replace('{keyword}', urlencode($_POST["searchText"]), $url);
$ch = curl_init();
$headers = array(
"Authorization: Basic " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$rs = curl_exec($ch);
echo($rs);
curl_close($ch);
return $rs;
}
?>
</form>
</body>
</html>发布于 2012-08-22 14:37:16
我不得不补充
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);为了使它工作,至少在我的本地副本(WAMP)。
希望能帮上忙,我一整天都在搞这个。
发布于 2012-07-17 21:13:27
$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query='; 这是问题的一部分
这不会给url bing正在寻找的
e.g. https://api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27love+message%27 它会是
https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27love+message%27 而您想要的是一个web,而不是一个图像搜索,而且格式和其他参数应该在查询之后。
“图像”应该是“网络”。
我只是花了三天的时间想让这件事起作用。
https://stackoverflow.com/questions/11451178
复制相似问题