【问题标题】:Bing Web Search API v7 doesn't seem to workBing Web Search API v7 似乎无法正常工作
【发布时间】:2019-03-11 22:31:20
【问题描述】:

我已将 JavaScript 代码复制到 JavaScript 文件中并遵循文件夹格式,但它似乎不起作用。我已经添加了我的订阅密钥,并且我已经改变了它所说的端点。我想要的只是让某人通过栏搜索某些内容并单击一个按钮,然后它会显示结果。你知道如何正确地做到这一点。我几乎尝试了所有方法。

我正在尝试做 .PHP 版本

https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/php

【问题讨论】:

  • 还有谁知道我将如何实现这个
    变成一个按钮和搜索栏?

标签: php css azure api bing


【解决方案1】:

我尝试按照官方教程Quickstart: Use PHP to call the Bing Web Search API 复制代码并使用我的密钥运行。和你的不一样,结果如下图。

然后,我知道您的问题可能与其他 SO 线程 Microsoft translator azure are returning null with PHP API 相同。

在 Azure 上,用于 Bing Web Search 的认知服务有两种 API 类型,包括 All Cognitive Services(多合一订阅)和 Bing Search v7(仅限 Bing 搜索),如下图所示。

图 1. API 类型 All Cognitive Services(多合一订阅)哪个端点取决于您的服务区域 (Location)

图 2. API 类型 Bing Search v7(仅限 Bing 搜索)

他们使用不同的端点,例如,在我的位置Southeast AsiaAll Cognitive Service中,端点是https://southeastasia.api.cognitive.microsoft.com/。对于此 API 类型的 Bing Search v7,必须将最终端点更改为 https://southeastasia.api.cognitive.microsoft.com/bing/v7.0/search

所以PHP的官方代码只需要改两行代码,如下,我测试成功后。

  1. 使用API​​类型All Cognitive Services,代码为:

    $accessKey = '<your key for API type of All Cognitive Services>';
    $endpoint ='https://<the region of your cognitive service like southeastasia>.api.cognitive.microsoft.com/bing/v7.0/search'; 
    
  2. 使用API​​类型Bing Search v7,代码为:

    $accessKey = '<your key for API type of Bing Search v7>';
    $endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search';
    

对于您评论中的第二个问题,HTML网页和php脚本的简单解决方案如下。

searchbar.html

<!DOCTYPE html>
<html>
<body>
    <form name="bing" method="POST" action="search.php">
        <input type="text" name="term">
        <input type="submit" value="Search">
    </form>
</body>
</html>

search.php:只需更改$term的官方代码即可。

<?php
// as above
$accessKey = '<the key of your Cognitive Services>';
$endpoint = '<the endpoint of your Cognitive Services>';

// The `term` index is mapping to the `name` value of type `text` of tag `input` name `term`. 
// Case when use `method="POST"` in form tag, the value of `term` got by `$_POST` method
// Or case when use `GET` method, it change to `$_GET`.
$term = $_POST['term']; 

function BingWebSearch ($url, $key, $query) {
    /* Prepare the HTTP request.
     * NOTE: Use the key 'http' even if you are making an HTTPS request.
     * See: http://php.net/manual/en/function.stream-context-create.php.
     */
    $headers = "Ocp-Apim-Subscription-Key: $key\r\n";
    $options = array ('http' => array (
                          'header' => $headers,
                           'method' => 'GET'));

    // Perform the request and get a JSON response.
    $context = stream_context_create($options);
    $result = file_get_contents($url . "?q=" . urlencode($query), false, $context);
    echo $result;
    // Extract Bing HTTP headers.
    $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);
}

// Validates the subscription key.
if (strlen($accessKey) == 32) {

    print "Searching the Web for: " . $term . "\n";
    // Makes the request.
    list($headers, $json) = BingWebSearch($endpoint, $accessKey, $term);

    print "\nRelevant Headers:\n\n";
    foreach ($headers as $k => $v) {
        print $k . ": " . $v . "\n";
    }
    // Prints JSON encoded response.
    print "\nJSON Response:\n\n";
    echo $json;
    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");

}
?>

【讨论】:

  • 那么完整的 search.php 会是什么样子?
  • @UKzRomulus 我将我的search.php 代码更新为完整。
猜你喜欢
  • 2021-03-24
  • 2017-09-04
  • 2012-05-06
  • 2013-02-17
  • 2017-10-09
  • 2017-08-03
  • 2014-05-02
  • 2011-09-11
  • 2016-02-11
相关资源
最近更新 更多