1

我的代码:

$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=[{"mid": null,"name": null,"topics:mid|=":[" /m/0gn30","/m/0tc7"]}]&callback=myfuncname&key=[key]) [function.file-get-contents]:打开流失败:HTTP 请求失败!第59[文件名]中的 HTTP/1.0 400 错误请求

如果我将 URL 直接粘贴到浏览器中,我会毫无问题地返回结果!所以我假设它是我调用服务的方式?

4

2 回答 2

1

尝试使用 curl 代替,如下所示:

$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);
于 2012-05-21T23:09:44.263 回答
1

您是否在 php.ini 中将 allow_url_fopen 设置为“on”?你设置cookie有什么原因吗?

于 2012-05-21T20:34:53.410 回答