nup
nup

Reputation: 368

Does the Bing Search API demo key have any limitations?

Does the Bing Search API (images) demo key have any limitations? On the test page (https://azure.microsoft.com/ru-ru/services/cognitive-services/bing-image-search-api/), the search phrase produces a lot of results, but in my script there are no results at all. Here is the script itself:

function getImages($query) {
    $query = str_replace(' ', '+', $query);
    $url = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=' . $query . '&safesearch=strict';
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_TIMEOUT, '1'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: multipart/form-data',
        'Ocp-Apim-Subscription-Key: KEY'
    ));

    $result = curl_exec($curl);
    $result = json_decode($result, true);

    return $result['value'];
}

Upvotes: 1

Views: 269

Answers (1)

Jason Pan
Jason Pan

Reputation: 21838

Reason:

The value of CURLOPT_TIMEOUT too short.

Please modify the value of CURLOPT_TIMEOUT to >=5, and then continue the test.

In postman, the sample request need 1.074s. I change the value of CURLOPT_TIMEOUT. It works for me.

enter image description here

Code:

<?php
function getImages($query) {
    $query = str_replace(' ', '+', $query);
    $url = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=' . $query . '&safesearch=strict';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_TIMEOUT, '5'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: multipart/form-data',
        'Ocp-Apim-Subscription-Key: ********'
    ));
    $result = curl_exec($curl);
    $result = json_decode($result, true);
    return $result['value'];
}
$result=getImages("cats");
print_r($result);
?>

Run it online.

Open the site to test code online. Paste code, and run it.

Then you can check the result between postman and online.

enter image description here

Upvotes: 1

Related Questions