微软自己的新Bing API示例不起作用。我试过很多方面,它只是表明:
服务器错误
401 - 未经授权:由于凭证无效,访问被拒绝 您无权查看此目录或页面 使用您提供的凭据。
示例在官方文档中给出的编码如下所示,它在
分解'proxy' => 'tcp://127.0.0.1:8888',
我100%确定我的密钥是正确的,当我在浏览器网址中输入它时,它工作正常,即
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>
我同时尝试过谷歌API和雅虎API,但这些都没有那么困难。
答案 0 :(得分:9)
这是使用CURL的正确编码在BING API中执行此操作,应用CURL方法而不是file_get_contents,它无法将正确的身份验证信息从Linux客户端传递到BING服务。
<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>
答案 1 :(得分:3)
我必须添加
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
为了使其成功,至少在我的本地副本(WAMP)中。
希望它有所帮助,我一直在搞乱这一点。
答案 2 :(得分:2)
$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
虽然您希望网络不是图像搜索,而且格式和其他参数也可以在查询之后
“image”应为“web”
我花了3天的时间试图让它发挥作用。
答案 3 :(得分:0)
我刚刚发布了一个如何使用Unirest Library在这里连接到Bing / Azure API的示例:https://stackoverflow.com/a/20096151/257815