2012-08-06 34 views
0

我一直在将应用程序从旧的Bing API移植到新的应用程序。看到了几篇关于PHP在PHP中如何工作的文章,我得到了关于它的认证问题。新的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); 

有没有人有什么想法可以触发au认证失败?

+0

可能重复(http://stackoverflow.com/questions/10844463/bing-search-api-and-azure) – ManseUK 2012-08-07 10:16:14

+0

不幸的线程没有帮助,尝试了所有的解决方案,他们都没有让我到任何地方 – 2012-08-07 10:17:50

+0

你检查了标题以确保密钥实际存在吗? – ManseUK 2012-08-07 10:20:41

回答

0

另一位团队成员更改了一个变量的名称,我想我应该马上进行检查。 $this->Appid应该是$this->APPID

正如我在评论ManseUK说,虽然微软可以提供缺少的应用程序ID一个更好的错误消息,有件事沿着“无效的appid”的线路,以帮助缩小问题下

1

这里的工作示例的搜索API只需将您的访问密钥替换为“XXXX”即可。连我浪费了不少时间得到它的工作使用卷曲,但它是在本地没有“CURLOPT_SSL_VERIFYPEER”的原因:(

$url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=%27xbox%27'; 
$process = curl_init($url); 
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); 
0

有人建议减少流量,提高速度:
添加Accept-Encoding: gzip和删除名称在AUTH。base64_encode(':' . $this->APPID)是你所需要的。

[Bing搜索API和天青]的