3
votes

When submitting a search on my bing custom search API it is returning 404 not found.

I have changed the API key and endpoint to my details My endpoint is: https://fruitbox-search.cognitiveservices.azure.com/bing/v7.0

// Replace with a valid subscription key from your Azure account.

$accessKey = 'MY-KEY-HERE';
$endpoint = 'https://fruitbox-search.cognitiveservices.azure.com/bing/v7.0';
$term = 'Microsoft Cognitive Services';

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 receive a response.
    $context = stream_context_create($options);
    $result = file_get_contents($url . "?q=" . urlencode($query), false, $context);

    // 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);
}

I am getting the following errors in my console:

GET https://fruitbox-search.cognitiveservices.azure.com/bing/v7.0?q=test&mkt=en-US&SafeSearch=strict&promote=webpages&answerCount=9&count=25&offset=0&textDecorations=true&textFormat=HTML 404 (Resource Not Found) - VM523:1

Refused to get unsafe header "BingAPIs-TraceId" - script.js:264

1
Did you check, Is that working fine with a postman ?Neel Rathod
I may be sounding dumb but how do I do that?Aaron Davis
I have checked this with getpostman and its returning 404 not foundAaron Davis
Then check API documentation, Verify that are you pass data as per It's documentation or notNeel Rathod

1 Answers

2
votes

I have solved this, solution is below:

I had to set my endpoint to: https://fruitbox-search.cognitiveservices.azure.com/bing/v7.0/search

It was missing the /search at the end.