Bing Web Search API v5.0的基础知识

问题描述 投票:0回答:2

我正在尝试从基础开始,并从Azure API for Bing的Web搜索中获取任何返回的结果。我已经通过他们的沙箱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
2个回答
2
投票

我发现的工作代码有望帮助您入门:

$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);

0
投票

API参考页面(https://dev.cognitive.microsoft.com/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d)包含最常用编程语言底部的代码片段。

通常,您可以通过单击每个API页面顶部附近的“API Reference”teal按钮找到每个Cognitive Services API的参考页面。

© www.soinside.com 2019 - 2024. All rights reserved.