必应搜索API和Azure [英] Bing search API and Azure

查看:87
本文介绍了必应搜索API和Azure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式在Microsoft Bing搜索引擎上执行搜索.

I am trying to programatically perform a search on Microsoft Bing search engine.

这是我的理解:

  • 有一个Bing Search API 2.0,即将被替换(2012年8月1日)
  • 新API被称为Windows Azure Marketplace.
  • 两者使用不同的URL.

在旧的API(Bing Search API 2.0)中,您在URL中指定了一个密钥(应用程序ID),该密钥将用于验证请求.只要在URL中将键作为参数,就可以获取结果.

In the old API (Bing Search API 2.0), you specify a key (Application ID) in the URL, and such key will be used to authenticate the request. As long as you have the key as a parameter in the URL, you can obtain the results.

在新的API(Windows Azure Marketplace)中,您不要在URL中包含密钥(帐户密钥").而是输入查询URL,然后服务器将要求您提供凭据.使用浏览器时,会出现一个弹出窗口,要求您输入帐号名称和密码.指示是将帐户名保留为空白,然后在密码字段中插入密钥.

In the new API (Windows Azure Marketplace), you do NOT include the key (Account Key) in the URL. Instead, you put in a query URL, then the server will ask for your credentials. When using a browser, there will be a pop-up asking for a/c name and password. Instruction was to leave the account name blank and insert your key in the password field.

好吧,我已经做了所有这些事情,并且可以在浏览器页面上看到搜索结果的JSON格式.

Okay, I have done all that and I can see a JSON-formatted results of my search on my browser page.

如何在PHP中以编程方式执行此操作?我尝试从Microsoft MSDN库中搜索文档和示例代码,但是我在错误的位置搜索,或者那里的资源极其有限.

How do I do this programmatically in PHP? I tried searching for the documentation and sample code from Microsoft MSDN library, but I was either searching in the wrong place, or there are extremely limited resources in there.

请问有人可以告诉我如何在PHP中在弹出窗口的密码字段中输入密钥"部分吗?

Would anyone be able to tell me how do you do the "enter the key in the password field in the pop-up" part in PHP please?

非常感谢.

推荐答案

新服务的文档可能会变得有些有趣-尤其是在MSDN中.我可以从此迁移指南上找到最清晰的解释. "https://datamarket.azure.com/dataset/8818F55E-2FE5-4CE3-A617-0B8BA8419F65" rel ="noreferrer">必应搜索API 页.最重要的是,迁移指南最后提供了一个很好的PHP简单示例.

Documentation for new services can get a bit interesting - especially in the rabbit-warren of MSDN. The most clear explanation I can find is on the Migration Guide from this Bing Search API page. Best of all the migration guide has a nice simple example in PHP towards the end.

好的,迁移指南是一个起点,但这不是最好的例子.这是两种对我有用的方法(没有代理,防火墙等干扰):

Alright, the migration guide is a starting point, but it isn't the best example. Here are two methods that work for me (no proxy, firewalls etc. interfering):

注意:需要启用" allow_url_fopen ".如果没有,您可以使用 ini_set (或更改php.ini等).

Note: 'allow_url_fopen' needs to be enabled for this to work. You can use ini_set (or change php.ini etc.) if it isn't.

if (isset($_POST['submit'])) 
{

    // Replace this value with your account key
    $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';            
    $ServiceRootURL =  'https://api.datamarket.azure.com/Bing/Search/';                    
    $WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';

    $cred = sprintf('Authorization: Basic %s', 
      base64_encode($accountKey . ":" . $accountKey) );

    $context = stream_context_create(array(
        'http' => array(
            'header'  => $cred
        )
    ));

    $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');

    $response = file_get_contents($request, 0, $context);

    $jsonobj = json_decode($response);

    echo('<ul ID="resultList">');

    foreach($jsonobj->d->results as $value)
    {                        
        echo('<li class="resultlistitem"><a href="' 
                . $value->URL . '">'.$value->Title.'</a>');
    }

    echo("</ul>");
}

使用cURL

如果已安装cURL,最近这是正常的情况:

Using cURL

If cURL is installed, which is normal these days:

<?php
  $query = $_POST['searchText'];

  $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  $serviceRootURL =  'https://api.datamarket.azure.com/Bing/Search/';  
  $webSearchURL = $serviceRootURL . 'Web?$format=json&Query=';

  $request = $webSearchURL . "%27" . urlencode( "$query" ) . "%27";

  $process = curl_init($request);
  curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($process, CURLOPT_USERPWD,  "$accountKey:$accountKey");
  curl_setopt($process, CURLOPT_TIMEOUT, 30);
  curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  $response = curl_exec($process);
  $response = json_decode($response);

  echo "<ol>";
  foreach( $response->d->results as $result ) {
    $url = $result->Url;
    $title = $result->Title;

    echo "<li><a href='$url'>$title</a></li>";
  }
  echo "</ol>";
?>

[WTS]将SearchWeb更改为搜索".

[WTS] changed SearchWeb to Search.

这篇关于必应搜索API和Azure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆