【问题标题】:Basics of Bing Web Search API v5.0Bing Web Search API v5.0 基础
【发布时间】:2016-12-24 19:33:38
【问题描述】:

我尝试从基础开始,并从 Azure API for Bing 的网络搜索中获取任何返回的结果。我已经通过他们的沙箱API Testing Console 成功地产生了结果,但是在实时环境中我没有得到任何地方。我熟悉 Bing API 上的 previous SO posts,但这些响应已有 4 或 5 年的历史,似乎并未引用当前的 API。

Azure docs 参考将 Ocp-Apim-Subscription-Key 标头设置为 API 密钥。当微软自己的文档appears very dated 出现在某些地方时,这令人沮丧。我敢肯定我是第一个抱怨这件事的人!

注意:在写这个问题时,我得到了一个可行的解决方案。我将继续发布带有工作代码的答案。 Bing API v5.0 的示例似乎少之又少。

【问题讨论】:

    标签: php azure bing-api


    【解决方案1】:

    我找到的工作代码有望帮助您入门:

    $accountKey = 'the_account_key';
    
    $url =  'https://api.cognitive.microsoft.com/bing/v5.0/search?q=billgates&count=10&offset=0&mkt=en-us&safesearch=Moderate';    
    
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Ocp-Apim-Subscription-Key: $accountKey"
      )
    );
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    $file = file_get_contents($url, false, $context);
    
    echo $file;
    

    这是原始响应。您需要解码 JSON 并使用该对象:

    $jsonobj = json_decode($file);
    

    【讨论】:

      【解决方案2】:

      API 参考页面 (https://dev.cognitive.microsoft.com/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d) 在底部包含大多数流行编程语言的代码 sn-ps。

      通常,您可以通过单击每个 API 页面顶部附近的“API 参考”蓝绿色按钮找到每个认知服务 API 的参考页面。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-19
        • 2013-06-06
        相关资源
        最近更新 更多