Quickstart: Search for images using the Bing Image Search REST API and PHP


On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to make your first call to the Bing Image Search API and receive a JSON response. The simple application in this article sends a search query and displays the raw results.

Although this application is written in PHP, the API is a RESTful Web service compatible with any programming language that can make HTTP requests and parse JSON.


For more information, see Azure AI services pricing - Bing Search API.

Create and initialize the application

To run this application, follow these steps:

  1. Make sure secure HTTP support is enabled in your php.ini file. For Windows, this file is located in C:\windows.

  2. Create a new PHP project in your favorite IDE or editor.

  3. Define the API endpoint, your subscription key, and search term. The endpoint can be the global endpoint in the following code, or the custom subdomain endpoint displayed in the Azure portal for your resource.

    $endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/images/search';
    // Replace the accessKey string value with your valid access key.
    $accessKey = 'enter key here';
    $term = 'tropical ocean';

Construct and perform an HTTP request

  1. Use the variables from the last step to prepare an HTTP request to the Image Search API.

    $headers = "Ocp-Apim-Subscription-Key: $key\r\n";
    $options = array ( 'http' => array (
                            'header' => $headers,
                            'method' => 'GET' ));
  2. Send the web request and get the JSON response.

    $context = stream_context_create($options);
    $result = file_get_contents($url . "?q=" . urlencode($query), false, $context);

Process and print the JSON

Process and print the returned JSON response.

$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);

Example JSON response

Responses from the Bing Image Search API are returned as JSON. This sample response has been truncated to show a single result.

"readLink":"images\/search?q=tropical ocean",
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?q=tropical ocean&FORM=OIIARP",
        "name":"My Life in the Ocean | The greatest WordPress.com site in ...",
        "contentSize":"897388 B",
            "text":"Tropical Beaches Desktop Wallpaper",
            "displayText":"Tropical Beaches Desktop Wallpaper",

Next steps

See also