連接Bing搜索API在嘗試創建與Windows Azure的新兵基於API的新API的請求,使用以下腓轉換的file_get_contents失敗使用Windows Azure的
$url= 'https://'.$this->m_host.'/Web?Query={keyword}&Adult=%27Off%27&$top=50&$format=Atom';
$url=str_replace('{keyword}', urlencode($this->m_keywords), $url);
// Replace this value with your account key
$accountKey = $this->key;
$WebSearchURL = $url;
$context = stream_context_create(array(
'http' => array(
'proxy' => 'tcp://127.0.0.1:8888',
'request_fulluri' => true,
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey)
)
));
$request = $WebSearchURL;
$response = file_get_contents($request, 0, $context);
print_r($response);
的代碼我得到以下錯誤。
Warning: file_get_contents() [function.file-get-contents]:
Couldn't connect to server in /home/xxxxx on line 43
Warning: file_get_contents(https://api.datamarket.azure.com/
failed to open stream: operation failed in /home/xxxx/ bing_search.php on line 43
任何想法爲什麼這會失敗?或者最好使用CURL庫而不是file_get_contents()?
按照這篇文章中的說明我註釋了2參數 [link] http://stackoverflow.com/questions/10845672/windows-azure-authentication-for-bing-search-in-php[link](http :http://stackoverflow.com/questions/10845672/windows-azure-authentication-for-bing-search-in-php) 'proxy'=>'tcp://127.0.0.1:8888', 'request_fulluri' => true, 但我仍然得到 erver錯誤 401 - 未經授權:由於憑據無效,訪問被拒絕。 您無權使用您提供的憑據查看此目錄或頁面。 – mahen3d
嘗試在瀏覽器中https://api.datamarket.azure.com/Bing/SearchWeb/?query=test並輸入您的密鑰作爲用戶名和密碼,以驗證您是否擁有有效憑據。 –
是的,它適用於直接網址一切都好,只是連接正在中止不知道爲什麼可能是網絡問題..我來自wget工作的網址..但在PHP它不工作 – mahen3d