我一直在将工作中的应用程序从旧的 Bing API 移植到新的应用程序。看过几篇关于新版本如何在 PHP 中工作的帖子后,我遇到了身份验证问题。
这是 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
反过来,这是我用来与 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);
自从尝试了这个我现在也尝试过
$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);
有没有人知道什么可能会触发身份验证失败?