-2

我正在尝试使用 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;
4

1 回答 1

2

我只是跳到文档中,您可以使用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);
于 2013-01-18T09:46:49.843 回答