-2

I am trying to search some images with bing api key. I wrote this code but it don't work, and it not return some error. I think it is an api error...can you help me?

$accountKey = 's+s/s=';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';

// Get the query. Default to 'sushi'.
$query = ($_GET['q']) ? $_GET['q'] : 'sushi';

// Get the service operation. Default to Web.
$serviceOp = ($_GET['sop']) ? $_GET['sop'] : 'Web';

// Get the market. Default to en-us.
$market = ($_GET['market']) ? $_GET['market'] : 'en-us';

$ServiceRootURL =  'https://api.datamarket.azure.com/Bing/SearchWeb/';  
$WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';

$request = $WebSearchURL . urlencode( '\'' . $query. '\'');
echo $request;
//$requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query&Market=$market";

$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);
$response = curl_exec($process);
$jsonobj = json_decode($response);
echo "<br>Ris = ".$jsonobj;
michele
  • 26,348
  • 30
  • 111
  • 168

1 Answers1

2

I just jump into the documentation and you can use file_get_contents instead of cURL.

<?php
$accountKey = 'xxxxx';

$auth = base64_encode("$accountKey:$accountKey");

$data = array(
  'http'            => array(
  'request_fulluri' => true,
  'ignore_errors'   => true,
  'header'          => "Authorization: Basic $auth")
);

$context   = stream_context_create($data);
$query     = isset($_GET['q']) ? $_GET['q'] : 'sushi';
$serviceOp = isset($_GET['sop']) ? $_GET['sop'] : 'Web';
$market    = isset($_GET['market']) ? $_GET['market'] : 'en-us';

$ServiceRootURL = 'https://api.datamarket.azure.com/Bing/Search/';  
$WebSearchURL   = $ServiceRootURL . 'Web?$format=json&Query=';

$request = $WebSearchURL . urlencode( '\'' . $query. '\'');

// Get the response from Bing.
$response = file_get_contents($request, 0, $context);
var_dump($response);
j0k
  • 22,600
  • 28
  • 79
  • 90
  • 1
    The authorization type you provided is not supported. Only Basic and OAuth are supported this error is returned with your and mine script..... – michele Jan 18 '13 at 09:49
  • @michele Have you updated the `$accountKey` with yours? Btw, the `ServiceRootURL` you are using is wrong, it should end up with `Search` instead of `SearchWeb`. – j0k Jan 18 '13 at 09:56
  • thanks, url was incorrect! – michele Jan 18 '13 at 10:03