首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bing API相关搜索

Bing API相关搜索
EN

Stack Overflow用户
提问于 2019-04-29 18:14:00
回答 1查看 349关注 0票数 0

如何让相关搜索包含在Bing search API的响应中?

根据这里的文档,我正在尝试应用具有值RelatedSearches的responseFilter:https://docs.microsoft.com/en-us/rest/api/cognitiveservices/bing-web-api-v7-reference#relatedsearchanswer

下面是我的代码,基于Bing的示例。我将"&responseFilter=RelatedSearches“添加到由file_get_contents()获取的URL中。

我得到的错误是:

代码语言:javascript
运行
复制
PHP Warning:  file_get_contents(https://api.cognitive.microsoft.com/bing/v7.0/search?responseFilter=RelatedSearches&q=iphone+case): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

显然,我的应用是错误的。我可以做些什么来使其正常工作?

代码语言:javascript
运行
复制
<?php
$accessKey = 'abc123';
$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search';
$term = 'burrito recipe';

function BingWebSearch ($url, $key, $query) {
    $headers = "Ocp-Apim-Subscription-Key: $key\r\n";
    $options = array ('http' => array (
                        'header' => $headers,
                        'method' => 'GET'));
    $context = stream_context_create($options);
    $result = file_get_contents($url . "?q=" . urlencode($query) . "&responseFilter=RelatedSearches", false, $context);
    $headers = array();
    foreach ($http_response_header as $k => $v) {
        $h = explode(":", $v, 2);
        if (isset($h[1]))
            if (preg_match("/^BingAPIs-/", $h[0]) || preg_match("/^X-MSEdge-/", $h[0]))
                $headers[trim($h[0])] = trim($h[1]);
    }
    return array($headers, $result);
}

if (strlen($accessKey) == 32) {
    print "Searching the Web for: " . $term . "\n";
    list($headers, $json) = BingWebSearch($endpoint, $accessKey, $term);
    print "\nRelevant Headers:\n\n";
    foreach ($headers as $k => $v) {
        print $k . ": " . $v . "\n";
    }
    print "\nJSON Response:\n\n";
    echo json_encode(json_decode($json), JSON_PRETTY_PRINT);

} else {
    print("Invalid Bing Search API subscription key!\n");
    print("Please paste yours into the source code.\n");
}
?>
EN

回答 1

Stack Overflow用户

发布于 2019-04-29 18:21:16

"403禁止“是一个提示。您没有权限请求RelatedSearches,除非您在S1实例上,如下所示:https://azure.microsoft.com/en-us/pricing/details/cognitive-services/search-api/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55901233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档