1

My Code:

$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);

Error: Warning: 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]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in [file name] on line 59

If I paste the URL directly into the browser I get the results back without any problem! So I assume its the way I'm calling the service?

txchou
  • 647
  • 1
  • 6
  • 15
  • See this thread: http://stackoverflow.com/questions/3535799/file-get-contents-failed-to-open-stream, http://stackoverflow.com/questions/1975461/file-get-contents-with-https – web-nomad May 21 '12 at 07:30

2 Answers2

1

Do you have allow_url_fopen set to "on" in php.ini? Is there a reason why you are setting a cookie?

  • Yes - on local host it's set to "on" already. On the remote server I don't think I have access to the php.ini file. Ignore the cookie bit it doesn't work with or without that line. – txchou May 22 '12 at 05:12
1

Try using curl instead, like this:

$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);
Shawn Simister
  • 4,613
  • 1
  • 26
  • 31
  • Okay, I am using cURL and I get no response. Blank. Even curl_error doesn't report any errors. How else can I test what's actually going wrong? – txchou May 23 '12 at 03:57
  • The code I gave doesn't echo anything to the screen. Its up to you to decide what you want it to display. Does your template expect some data in $json_output? If so, make sure that that variable is being set. – Shawn Simister May 24 '12 at 21:47