0

When try to create a new API request with the Windows Azure new Bing based API, Using the code below

$url= 'https://'.$this->m_host.'/Web?Query={keyword}&Adult=%27Off%27&$top=50&$format=Atom';     

        $url=str_replace('{keyword}', urlencode($this->m_keywords), $url);

        // Replace this value with your account key
                    $accountKey = $this->key;

                    $WebSearchURL = $url;

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

                    $request = $WebSearchURL;
                    $response = file_get_contents($request, 0, $context);

        print_r($response);

i get following error.

Warning: file_get_contents() [function.file-get-contents]: 
Couldn't connect to server in /home/xxxxx on line 43

Warning: file_get_contents(https://api.datamarket.azure.com/ 
failed to open stream: operation failed in /home/xxxx/ bing_search.php on line 43

Any idea why this fails ? or is it best to use the CURL Library than the file_get_contents() ?

mahen3d
  • 7,047
  • 13
  • 51
  • 103

4 Answers4

1

The below code works for me, it is to search news but it will work for web searches too.

Just replace appkey with your one, leave username as it is (i.e. username) since it is ignored by the server

function getBingResult($keyword)
{
    $credentials = "username:appkey";
    $url= "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=%27{keyword}%27". "&\$format=json";        
    $url=str_replace('{keyword}', urlencode($keyword), $url);
    $ch = curl_init();
    $headers = array(
       "Authorization: Basic " . base64_encode($credentials)
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($session, CURLOPT_VERBOSE, TRUE); 

    $rs = curl_exec($ch);
    $jsonobj = json_decode($rs);
    curl_close($ch);    
    return $jsonobj;
}

Testing the function:

$bingResult = getBingResult("John");
foreach($bingResult->d->results as $value)
{   
    echo '<pre>'."URL:". $value->Url.'</pre>';
    echo '<pre>'."Title:". $value->Title.'</pre>';
    echo '<pre>'."Description:". $value->Description.'</pre>';   
    echo '<pre>'."Source:". $value->Source.'</pre>';   
    echo '<pre>'."Date:". $value->Date.'</pre>';   
}
Littm
  • 4,923
  • 4
  • 30
  • 38
marco
  • 26
  • 1
0

Either file_get_contents or CURL will work for the Bing API, you can use what will work on your system and what you are comfortable with.

First I would check your server can connect to the Windows Azure server. Try running a ping and then a wget from the command line to see if it can. Do you go through a proxy? You'll need to set those details in your stream context.

I'm not sure what you have $this->m_host set to, but the new Bing API should be at either: https://api.datamarket.azure.com/Bing/Search/ or https://api.datamarket.azure.com/Bing/SearchWeb/. The URL https://api.datamarket.azure.com/Web comes back as invalid.

John C
  • 8,223
  • 2
  • 36
  • 47
  • as instructed in this post i commented out the 2 parameters [link] http://stackoverflow.com/questions/10845672/windows-azure-authentication-for-bing-search-in-php[link] (http://stackoverflow.com/questions/10845672/windows-azure-authentication-for-bing-search-in-php) 'proxy' => 'tcp://127.0.0.1:8888', 'request_fulluri' => true, but still i get erver Error 401 - Unauthorized: Access is denied due to invalid credentials. You do not have permission to view this directory or page using the credentials that you supplied. – mahen3d Jul 03 '12 at 06:35
  • Try going to https://api.datamarket.azure.com/Bing/SearchWeb/?query=test in your browser and entering your key as the username & password to verify you have valid credentials. – John C Jul 03 '12 at 06:47
  • yes it works for direct url all good, just the connection is making abort not sure why may be network problem.. i fount wget works for the url .. but in php it doesnt work for – mahen3d Jul 03 '12 at 06:55
  • $context = stream_context_create(array( 'http' => array( 'method'=>'GET', //'proxy' => 'tcp://127.0.0.1:8888', 'request_fulluri' => true, 'header' => "Authorization: Basic " . base64_encode(":".$this->key) ) )); $request = $url; $response = file_get_contents($request, 0, $context); – mahen3d Jul 03 '12 at 06:57
  • Have you tried with a dummy username? `'header' => "Authorization: Basic " . base64_encode("test:".$this->key)` – John C Jul 03 '12 at 07:10
0

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 :(

$process = curl_init('https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27');
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);
Kailash Yadav
  • 1,880
  • 2
  • 18
  • 37
0

1.) You do not need str_replace(). Use the var directly inside the url:
$url= 'https://'.$this->m_host.'/Web?Query='.urlencode($this->m_keywords).'&Adult=%27Off%27&$top=50&$format=Atom';

2.) You defined three different vars with the same value:
$WebSearchURL = $url;
$request = $WebSearchURL;

Use $url only.

3.) base64_encode($accountKey . ":" . $accountKey) can be reduced to base64_encode(":" . $accountKey)

4.) Add Accept-Encoding: gzip to your header to reduce traffic and raise speed.

5.) Your problem should be this line:
'proxy' => 'tcp://127.0.0.1:8888',

Remove it or change it to the correct value.

mgutt
  • 5,867
  • 2
  • 50
  • 77