I recently got an email from Microsoft saying that the Bing API was moving to the Windows Azure Marketplace. It seemed that the main difference between the new request was the authentication.

After reading many posts on forums, I found this:

$accountKey = '#########';
$api =  'https://api.datamarket.azure.com/Bing/Search/Web?$format=json&$top=8&Query=';
$context = stream_context_create(array(
    'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
));
$request = $api.'%27'.$q.'%27&$skip='.$start;
$result = file_get_contents($request, 0, $context);

However, I still get the error "The authorization type you provided is not supported. Only Basic and OAuth are supported".

Does anyone know how I can fix this. I have also tried cURL and that doesn't work. Thanks to anyone who can find me a solution.

4

There are 4 best solutions below

9
On

I think the URLs have changed. This code works. Note the URL in the first line:

$api = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?$format=json&$top=8&Query=';
$context = stream_context_create(array(
    'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
));
$q = 'test';
$request = $api.'%27'.$q.'%27';

echo file_get_contents($request, 0, $context);
0
On

I had the same problem which occured when I deployed a website to a new server. I think my hosting company disabled some functionality with file_get_contents to external links.

    $url = 'https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate?Text=%27'.urlencode($text).'%27&To=%27' . $to . '%27&From=%27' . $from . '%27&$top=100&$format=json';

    $accountKey = 'APIKEY';
    $handle = curl_init ($url);
    if ($handle) {
        $curlOptArr = array(
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            CURLOPT_USERPWD => $accountKey . ':' . $accountKey,
            CURLOPT_RETURNTRANSFER => TRUE

        );
        curl_setopt_array($handle, $curlOptArr);
        $response = curl_exec($handle);
        $data = json_decode($response,true);
        if (is_array($data)) {
            if (isset($data['d']['results'][0]['Text'])) {
                print $data['d']['results'][0]['Text'];
            } else {
                print false;
            }
        } else {
            print $text;
        }
        $errRet = curl_error($handle);
        curl_close($handle);
    }

This one works for me when using cURL.

1
On

Here is working example of Search API just replace your access key with "XXXX". Even i wasted quite a few hours to get it work using cURL but it was failing cause of "CURLOPT_SSL_VERIFYPEER" on local :(

$url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, "username:XXXX");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($process);

# Deliver
return $response;

# Have a great day!
curl_close($process);
0
On

I met the same problem, now fixed, the root_url has changed, it is now something like: https://user:[email protected]/Bing/SearchWeb/Web?Query=%27leo%20fender%27&Market=%27en-US%27&$top=50&$format=JSON">