尝试使用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()?
答案 0 :(得分:1)
以下代码适用于我,它是搜索新闻,但它也适用于网络搜索。
只需将appkey替换为您的appkey,保留用户名(即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>';
}
答案 1 :(得分:0)
file_get_contents
或CURL
适用于Bing API,您可以使用适用于您系统的内容以及您认为合适的内容。
首先,我会检查您的服务器是否可以连接到Windows Azure服务器。尝试从命令行运行ping
然后运行wget
以查看是否可以。你通过代理吗?您需要在流上下文中设置这些详细信息。
我不确定你设置的$this->m_host
是什么,但新的Bing API应该是:
https://api.datamarket.azure.com/Bing/Search/或https://api.datamarket.azure.com/Bing/SearchWeb/。网址https://api.datamarket.azure.com/Web返回无效。
答案 2 :(得分:0)
以下是Search API的工作示例,只需用&#34; XXXX&#34;替换您的访问密钥。即使我浪费了几个小时才能使用cURL工作,但它失败的原因是&#34; CURLOPT_SSL_VERIFYPEER&#34;在当地:(
$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);
答案 3 :(得分:0)
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',
将其删除或将其更改为正确的值。