UKz Romulus
UKz Romulus

Reputation: 1

Bing Web Search API v7 doesn't seem to work

I have copied the JavaScript code onto a JavaScript file and followed the folder format and it doesn't seem to work. I've added my subscription key and I've changed the endpoint like it states. All i want is for someone to search for something via a bar and click a button and it displays the results. Would you know how to do this properly. I've tried nearly everything.

I'm trying to do the .PHP version

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

Upvotes: 0

Views: 2448

Answers (1)

Peter Pan
Peter Pan

Reputation: 24138

I tried to follow the offical tutorial Quickstart: Use PHP to call the Bing Web Search API to copy code and run with my key. It did not work as yours and got the result as the figure below.

enter image description here

Then, I was aware of your issue which may be as same as the other SO thread Microsoft translator azure are returning null with PHP API.

On Azure, there are two API types of Cognitive Service for Bing Web Search include All Cognitive Services (All-in-one subscription) and Bing Search v7 (Only Bing Search), as the figures below.

Fig 1. API type All Cognitive Services (All-in-one subscription) which endpoint is depended on your service region (Location)

enter image description here

Fig 2. API type Bing Search v7 (Only Bing Search)

enter image description here

They are using different endpoints, for example, in my All Cognitive Service of Location Southeast Asia, the endpoint is https://southeastasia.api.cognitive.microsoft.com/. And for Bing Search v7 in this API type, the final endpoint must have to be changed with https://southeastasia.api.cognitive.microsoft.com/bing/v7.0/search.

So the offical code for PHP just need to be changed two code lines, as below, after I test success.

  1. For using API type All Cognitive Services, the code is:

    $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. For using API type Bing Search v7, the code is:

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

For your second question in your comment, the simple solution for HTML web page and php script is as below.

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: just need to change the offical code for $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");

}
?>

Upvotes: 1

Related Questions