2012-05-21 69 views
1

我的代碼:的遊離鹼MQL閱讀服務和PHP的file_get_contents

$api_url = "https://www.googleapis.com/freebase/v1-sandbox/mqlread?query="; 
$query = $freebase_query."&callback=myfuncname"."&key=".$cfg['fbkey']; 

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" 
) 
); 

$context = stream_context_create($opts); 
$json = file_get_contents($api_url.$query, false, $context, null); 
$display = $api_url.$query; 
$json_output = json_decode($json); 

錯誤: 警告:的file_get_contents(https://www.googleapis.com/freebase/v1-sandbox/mqlread?query= [ {"中間":空,"名稱":空,"主題:中間| = ":[" /米/ 0gn30 "," /米/ 0tc7 "]}] &回調= myfuncname &鍵= [鍵])[功能.file-get-contents]:無法打開流:HTTP請求失敗! HTTP/1.0 [文件名] 400錯誤的請求 上線

如果我直接將URL粘貼到瀏覽器中我得到的返回結果沒有任何問題!所以我認爲它是我打電話給服務的方式?

+0

看到這個線程:http://stackoverflow.com/questions/3535799/file-get-contents-failed-to-open-stream,HTTP: //stackoverflow.com/questions/1975461/file-get-contents-with-https –

回答

1

你已經設定的allow_url_fopen在php.ini爲「開」?你有什麼理由設置一個cookie?

+0

是 - 本地主機上它被設置爲「開」了。在遠程服務器上,我不認爲我有權訪問php.ini文件。忽略它不起作用的cookie位,或者沒有該行。 – txchou

1

嘗試使用捲曲,而不是像這樣:

$freebase_query = array(array('id' => NULL, 'name' => NULL, 'topics:mid|=' => array('/m/0gn30','/m/0tc7'))); 
$api_url = 'https://www.googleapis.com/freebase/v1-sandbox/mqlread'; 
$query = $api_url . '?query=' . urlencode(json_encode($freebase_query)); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $query); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$json_data = json_decode(curl_exec($ch), true); 
curl_close($ch); 
+0

好的,我正在使用cURL,但沒有得到任何迴應。空白。即使curl_error也不會報告任何錯誤。我還能如何測試實際發生了什麼問題? – txchou

+0

我給出的代碼沒有迴應任何東西到屏幕上。它取決於你決定你想要顯示的內容。您的模板是否需要$ json_output中的某些數據?如果是這樣,請確保正在設置該變量。 –