bing api搜索

时间:2013-01-18 09:35:11

标签: php curl bing-api

我正试图用bing api键搜索一些图像。 我写了这段代码,但它不起作用,并没有返回一些错误。 我认为这是一个api错误......你能帮助我吗?

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

1 个答案:

答案 0 :(得分:2)

我只是跳到the documentation,您可以使用file_get_contents代替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);