Php Bing搜索API和Azure
我正试图通过编程在微软Bing搜索引擎上执行搜索 以下是我的理解:Php Bing搜索API和Azure,php,search,bing,Php,Search,Bing,我正试图通过编程在微软Bing搜索引擎上执行搜索 以下是我的理解: Bing搜索API 2.0即将被取代(2012年8月1日) 新的API称为Windows Azure Marketplace 您为这两个应用程序使用不同的URL 在旧API(Bing Search API 2.0)中,您在URL中指定一个密钥(应用程序ID),该密钥将用于验证请求。只要您在URL中将键作为参数,就可以获得结果 在新的API(Windows Azure Marketplace)中,URL中不包含密钥(帐户密钥)
- Bing搜索API 2.0即将被取代(2012年8月1日)
- 新的API称为Windows Azure Marketplace
- 您为这两个应用程序使用不同的URL
提前多谢。有关新服务的文档可能会有点有趣,尤其是在MSDN的兔子圈中。我能找到的最清楚的解释是在本页的。最棒的是,迁移指南最后有一个很好的PHP简单示例 编辑:好的,迁移指南是一个起点,但它不是最好的例子。以下是两种适合我的方法(无代理、防火墙等): 使用文件获取内容 注意:需要启用“”才能使其工作。如果不是,您可以使用(或更改php.ini等)
if(isset($\u POST['submit']))
{
//将此值替换为您的帐户密钥
$accountKey='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
$ServiceRootURL='0https://api.datamarket.azure.com/Bing/Search/';
$WebSearchURL=$ServiceRootURL。“Web?$format=json&Query=”;
$cred=sprintf('授权:基本%s',
base64_编码($accountKey.:“$accountKey”);
$context=stream\u context\u create(数组(
“http'=>数组(
'header'=>$cred
)
));
$request=$WebSearchURL.urlencode('\'.$\u POST[“searchText”].'\'');
$response=file\u get\u contents($request,0,$context);
$jsonobj=json_decode($response);
echo(“”);
foreach($jsonobj->d->结果为$value)
{
echo(“- ”);
}
回声(“
”);
}
使用卷曲
如果安装了cURL,这在现在是正常的:
[WTS]将SearchWeb更改为Search。别忘了放以下内容:
base64_encode("ignored:".$accountKey)
而不是:
base64_encode($accountKey . ":" . $accountKey)
以上这些都不适合我。我正在运行MAMP,这可能是相关的。请尝试以下方法:
$accountKey='=';
函数sitesearch($query、$site、$accountKey、$count=NULL){
//代码来自http://go.microsoft.com/fwlink/?LinkID=248077
$context=stream\u context\u create(数组(
“http'=>数组(
'request_fulluri'=>true,
'header'=>“Authorization:Basic”.base64_编码($accountKey.:“$accountKey)
)
));
$ServiceRootURL='0https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Market=%27en-GB%27&;
$WebSearchURL=$ServiceRootURL.'$format=json&Query=';
$request=$WebSearchURL.urlencode(“$query”);//注意额外的单引号
if($count)$request.=“&\$top=$count”;//注意$top前面的美元符号——它不是一个变量!
返回json_decode(file_get_contents($request,0,$context),true);
}
$q=“查询”;
如果($q){
//获取搜索结果
$articles=sitesearch($q,$\u服务器['HTTP\u主机],$accountKey,100);
foreach($articles['d']['results']作为$article){
echo“”$article['Title']”。;
echo“”$article['Description']”。;
echo“”$article['Source']”。;
echo“”.strotime($article['Date'])。;
}
}
发件人:
这里是一个搜索API的工作示例,只需将“XXXX”替换为您的访问密钥即可。甚至我也浪费了好几个小时试图使用cURL让它工作,但由于本地的“CURLOPT_SSL_VERIFYPEER”(-所以确保正确设置了cURL选项),它失败了
$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, base64_encode("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);
下面是如何使用调用Bing/Azure API的示例 您可以通过在URL:
https://api.datamarket.azure.com/Bing/Search/v1/Web
或https://api.datamarket.azure.com/Bing/Search/v1/Image
注意:请求URL中的参数区分大小写。对于Bing,它们以大写字母开头。
它包含用于查询bing的python代码,这是根据最新的API(Windows Azure Marketplace)编写的
您可以使用下面的代码获取bing搜索结果
$acctKey = 'Your account key here';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
$query = 'Kitchen';
$serviceOp ='Image';
$market ='en-us';
$query = urlencode("'$query'");
$market = urlencode("'$market'");
$requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query&Market=$market";
$auth = base64_encode("$acctKey:$acctKey");
$data = array(
'http' => array(
'request_fulluri' => true,
'ignore_errors' => true,
'header' => "Authorization: Basic $auth"
)
);
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$response=json_decode($response);
echo "<pre>";
print_r($response);
echo "</pre>";
$acctKey='Your account key here';
$rootUri='1!'https://api.datamarket.azure.com/Bing/Search';
$query='Kitchen';
$serviceOp='Image';
$market='en-us';
$query=urlencode(“$query”);
$market=urlencode(“$market”);
$requestUri=“$rootUri/$serviceOp?\$format=json&Query=$Query&Market=$Market”;
$auth=base64_编码($acctKey:$acctKey”);
$data=数组(
“http'=>数组(
'request_fulluri'=>true,
“忽略错误”=>true,
'header'=>“授权:基本$auth”
)
);
$context=stream\u context\u create($data);
$response=file\u get\u contents($requestUri,0,$context);
$response=json_decode($response);
回声“;
打印成本($res)
require_once '/path/to/unirest-php/lib/Unirest.php';
$accountKey = "xxx";
$searchResults = Unirest::get("https://api.datamarket.azure.com/Bing/Search/v1/Composite",
array(),
array(
'Query' => '%27Microsoft%27',
'Sources' => '%27web%2Bimage%2Bvideo%2Bnews%2Bspell%27',
'$format' => 'json',
), $accountKey, $accountKey
);
// Results are here:
$objectArray = $searchResults->body->d->results;
$rawJson = $searchResults->raw_body;
# -*- coding: utf-8 -*-
import urllib
import urllib2
import json
def main():
query = "sunshine"
print bing_search(query, 'Web')
print bing_search(query, 'Image')
def bing_search(query, search_type):
#search_type: Web, Image, News, Video
key= 'YOUR_API_KEY'
query = urllib.quote(query)
# create credential for authentication
user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
credentials = (':%s' % key).encode('base64')[:-1]
auth = 'Basic %s' % credentials
url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/'+search_type+'?Query=%27'+query+'%27&$top=5&$format=json'
request = urllib2.Request(url)
request.add_header('Authorization', auth)
request.add_header('User-Agent', user_agent)
request_opener = urllib2.build_opener()
response = request_opener.open(request)
response_data = response.read()
json_result = json.loads(response_data)
result_list = json_result['d']['results']
print result_list
return result_list
if __name__ == "__main__":
main()
$acctKey = 'Your account key here';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
$query = 'Kitchen';
$serviceOp ='Image';
$market ='en-us';
$query = urlencode("'$query'");
$market = urlencode("'$market'");
$requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query&Market=$market";
$auth = base64_encode("$acctKey:$acctKey");
$data = array(
'http' => array(
'request_fulluri' => true,
'ignore_errors' => true,
'header' => "Authorization: Basic $auth"
)
);
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$response=json_decode($response);
echo "<pre>";
print_r($response);
echo "</pre>";