当尝试使用Windows Azure新的基于Bing的API创建新的API请求时,请使用下面的代码
$url= 'https://'.$this->m_host.'/Web?Query={keyword}&Adult=%27Off%27&$top=50&$format=Atom';
$url=str_replace('{keyword}', urlencode($this->m_keywords), $url);
// Replace this value with your account key
$accountKey = $this->key;
$WebSearchURL = $url;
$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;
$response = file_get_contents($request, 0, $context);
print_r($response);
我收到以下错误。
Warning: file_get_contents() [function.file-get-contents]:
Couldn't connect to server in /home/xxxxx on line 43
Warning: file_get_contents(https://api.datamarket.azure.com/
failed to open stream: operation failed in /home/xxxx/ bing_search.php on line 43
知道为什么这会失败吗?还是最好使用 CURL 库而不是 file_get_contents() ?
下面的代码对我有用,它用于搜索新闻,但它也适用于网络搜索。
只需将应用程序密钥替换为您的密钥,保持用户名不变(即 username
),因为它被服务器忽略
function getBingResult($keyword)
{
$credentials = "username:appkey";
$url= "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=%27{keyword}%27". "&'$format=json";
$url=str_replace('{keyword}', urlencode($keyword), $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);
curl_setopt($session, CURLOPT_VERBOSE, TRUE);
$rs = curl_exec($ch);
$jsonobj = json_decode($rs);
curl_close($ch);
return $jsonobj;
}
测试函数:
$bingResult = getBingResult("John");
foreach($bingResult->d->results as $value)
{
echo '<pre>'."URL:". $value->Url.'</pre>';
echo '<pre>'."Title:". $value->Title.'</pre>';
echo '<pre>'."Description:". $value->Description.'</pre>';
echo '<pre>'."Source:". $value->Source.'</pre>';
echo '<pre>'."Date:". $value->Date.'</pre>';
}
file_get_contents
或CURL
都可以用于必应 API,您可以使用适用于您的系统以及您熟悉的内容。
首先,我会检查您的服务器是否可以连接到Windows Azure服务器。尝试从命令行运行ping
,然后运行wget
,看看是否可以。你通过代理吗?您需要在直播上下文中设置这些详细信息。
我不确定您$this->m_host
设置了什么,但新的必应 API 应该位于:https://api.datamarket.azure.com/Bing/Search/或 https://api.datamarket.azure.com/Bing/SearchWeb/。网址 https://api.datamarket.azure.com/Web 返回为无效。
这是搜索 API 的工作示例,只需将您的访问密钥替换为"XXXX"。即使我浪费了相当多的时间来使用它使用 cURL 工作,但它失败了,导致本地:("CURLOPT_SSL_VERIFYPEER
$process = curl_init('https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27');
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);
1.) 你不需要 str_replace()。直接在 url:
中使用 var $url= 'https://'.$this->m_host.'/Web?Query='.urlencode($this->m_keywords).'&Adult=%27Off%27&$top=50&$format=Atom';
2.) 您定义了三个具有相同值的不同变量:
$WebSearchURL = $url;
$request = $WebSearchURL;
仅使用$url
。
3.)base64_encode($accountKey . ":" . $accountKey)
可以减少到base64_encode(":" . $accountKey)
4.) 在标题中添加Accept-Encoding: gzip
以减少流量并提高速度。
5.) 你的问题应该是这一行:
'proxy' => 'tcp://127.0.0.1:8888',
删除它或将其更改为正确的值。