我最近收到了一封来自微软的电子邮件,称Bing API正在转向Windows Azure Marketplace。似乎新请求之间的主要区别在于身份验证。
在论坛上阅读了很多帖子后,我发现了这个:
$accountKey = '#########';
$api = 'https://api.datamarket.azure.com/Bing/Search/Web?$format=json&$top=8&Query=';
$context = stream_context_create(array(
'http' => array(
'request_fulluri' => true,
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
)
));
$request = $api.'%27'.$q.'%27&$skip='.$start;
$result = file_get_contents($request, 0, $context);
但是,我仍然收到错误“您不支持您提供的授权类型。仅支持Basic和OAuth。”
有谁知道如何解决这个问题。我也尝试过cURL,但这不起作用。 感谢能找到解决方案的任何人。
答案 0 :(得分:3)
我认为网址已经改变了。这段代码有效。请注意第一行中的URL:
$api = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?$format=json&$top=8&Query=';
$context = stream_context_create(array(
'http' => array(
'request_fulluri' => true,
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
)
));
$q = 'test';
$request = $api.'%27'.$q.'%27';
echo file_get_contents($request, 0, $context);
答案 1 :(得分:2)
以下是Search API的工作示例,只需将您的访问密钥替换为“XXXX”即可。即使我浪费了几个小时才能使用cURL工作,但它在本地出现“CURLOPT_SSL_VERIFYPEER”失败:(
$url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, "username:XXXX");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($process);
# Deliver
return $response;
# Have a great day!
curl_close($process);
答案 2 :(得分:0)
我遇到了同样的问题,现在修复了,root_url已经改变,现在是这样的: https://user:yourAccountKey@api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27leo%20fender%27&Market=%27en-US%27& $顶部= 50&安培; $格式= JSON“>
答案 3 :(得分:-1)
当我将网站部署到新服务器时遇到了同样的问题。我认为我的托管公司已将file_get_contents的一些功能禁用到外部链接。
$url = 'https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate?Text=%27'.urlencode($text).'%27&To=%27' . $to . '%27&From=%27' . $from . '%27&$top=100&$format=json';
$accountKey = 'APIKEY';
$handle = curl_init ($url);
if ($handle) {
$curlOptArr = array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $accountKey . ':' . $accountKey,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($handle, $curlOptArr);
$response = curl_exec($handle);
$data = json_decode($response,true);
if (is_array($data)) {
if (isset($data['d']['results'][0]['Text'])) {
print $data['d']['results'][0]['Text'];
} else {
print false;
}
} else {
print $text;
}
$errRet = curl_error($handle);
curl_close($handle);
}
使用cURL时,这个适用于我。