2012-07-03 13 views
0

連接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()?

回答

1

下面的代碼適用於我,它是搜索新聞,但它也可以用於網絡搜索。

只需用一個取代的AppKey,留下用戶名,因爲它是(即username),因爲它是由服務器

function getBingResult($keyword) 
{ 
    $credentials = "username:appkey"; 
    $url= "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=%27{keyword}%27". "&\$format=json";   
    $url=str_replace('{keyword}', urlencode($keyword), $url); 
    $ch = curl_init(); 
    $headers = array(
     "Authorization: Basic " . base64_encode($credentials) 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($session, CURLOPT_VERBOSE, TRUE); 

    $rs = curl_exec($ch); 
    $jsonobj = json_decode($rs); 
    curl_close($ch);  
    return $jsonobj; 
} 

測試功能忽略:

$bingResult = getBingResult("John"); 
foreach($bingResult->d->results as $value) 
{ 
    echo '<pre>'."URL:". $value->Url.'</pre>'; 
    echo '<pre>'."Title:". $value->Title.'</pre>'; 
    echo '<pre>'."Description:". $value->Description.'</pre>'; 
    echo '<pre>'."Source:". $value->Source.'</pre>'; 
    echo '<pre>'."Date:". $value->Date.'</pre>'; 
} 
0

file_get_contentsCURL都可以用於Bing API,您可以使用適用於您的系統的功能以及您感到滿意的功能。

首先我會檢查你的服務器是否可以連接到Windows Azure服務器。嘗試從命令行運行ping,然後運行wget以查看是否可以。你是否通過代理?您需要在流上下文中設置這些詳細信息。

我不知道你有什麼$this->m_host集,但新的必應API應該是在任: https://api.datamarket.azure.com/Bing/Search/https://api.datamarket.azure.com/Bing/SearchWeb/。網址https://api.datamarket.azure.com/Web回到無效狀態。

+0

按照這篇文章中的說明我註釋了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

+0

嘗試在瀏覽器中https://api.datamarket.azure.com/Bing/SearchWeb/?query=test並輸入您的密鑰作爲用戶名和密碼,以驗證您是否擁有有效憑據。 –

+0

是的,它適用於直接網址一切都好,只是連接正在中止不知道爲什麼可能是網絡問題..我來自wget工作的網址..但在PHP它不工作 – mahen3d

0

這裏是工作示例搜索API只需將您的訪問密鑰替換爲「XXXX」即可。連我浪費了不少時間得到它的工作使用捲曲,但它是在本地沒有「CURLOPT_SSL_VERIFYPEER」的原因:(

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

1)你不需要str_replace()函數。直接使用VAR的網址內:
$url= 'https://'.$this->m_host.'/Web?Query='.urlencode($this->m_keywords).'&Adult=%27Off%27&$top=50&$format=Atom';

2)你用相同的值來定義三種不同的增值經銷商:
$WebSearchURL = $url;
$request = $WebSearchURL;

使用$url只。

3)base64_encode($accountKey . ":" . $accountKey)可以降低到base64_encode(":" . $accountKey)

4)添加Accept-Encoding: gzip到你的頭,以減少交通事故,提高速度。

5)你的問題應該是這一行:
'proxy' => 'tcp://127.0.0.1:8888',

刪除,或將其更改爲正確的值。