Daniel Groves
Daniel Groves

Reputation: 472

New Bing API Unsupported Authentication in PHP

I have been porting an application at work from the old Bing API to the new one. Having seen several posts about how the new one works in PHP I'm getting Authentication issues with it.

This is the error that is returned by the URL:

Warning: file_get_contents(https://api.datamarket.azure.com/Bing/Search/Image?$format=json&Query=%27%27) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 The authorization type you provided is not supported. Only Basic and OAuth are supported in /home/krem81/public_html/classes/class.BingSearch.php on line 178

And in turn, this is the function that I am using to interact with the API:

$accountKey = $this->Appid;

$ServiceRootURL =  "https://api.datamarket.azure.com/Bing/Search/";

$WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

$context = stream_context_create(array(
    'http' => array(
        'request_fulluri' => true,
        'header'  => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
    )
));

$request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');

echo($request);

$this->response = file_get_contents($request, 0, $context);

$this->results = json_decode($this->response);

Since trying this I have also now tried

    $accountKey = $this->Appid;

    $ServiceRootURL =  "https://api.datamarket.azure.com/Bing/Search/";

    $WebSearchURL = $ServiceRootURL . 'Image?$format=json&Query=';

    $request = $WebSearchURL . urlencode( '\'' . $this->keyword . '\'');

    echo($request);

    $process = curl_init($request);
    curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($process, CURLOPT_USERPWD,  $accountKey . ":" . $accountKey);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);

    $this->response = curl_exec($process);

    var_dump($this->response);

    $this->results = json_decode($this->response);

    var_dump($this->results);

Does anyone have any ideas as to what could be triggering the authentication to fail?

Upvotes: 0

Views: 1988

Answers (3)

mgutt
mgutt

Reputation: 6177

A suggestion to reduce traffic and raise speed:
Add Accept-Encoding: gzipand remove the name in the auth. base64_encode(':' . $this->APPID) is all you need.

Upvotes: 0

Kailash Yadav
Kailash Yadav

Reputation: 1930

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

Upvotes: 1

Daniel Groves
Daniel Groves

Reputation: 472

Another team member changed the name of a variable that I guess I really should have checked straight away. $this->Appid should have been $this->APPID.

As I said to ManseUK in the comments though Microsoft could provide a better error message for the missing app id though, something along the lines of 'invalid appid' to help narrow the issue down

Upvotes: 0

Related Questions