2012-05-21 25 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中的某些数据?如果是这样,请确保正在设置该变量。 –