0

I want to make the following API call in PHP:

https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29

How can I do so without using curl?

Blue Sky
  • 293
  • 1
  • 5
  • 14

4 Answers4

1

Use the PHP method: file_get_contents(): http://us1.php.net/file_get_contents

Fabien Warniez
  • 2,731
  • 1
  • 21
  • 30
1

This returns json. You should use json_decode() and file_get_contents() for this.

Example:

<?php

$file = file_get_contents("https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29");

$json = json_decode($file, true);

echo "<pre>";
print_r($json);
echo "</pre>";

?>
  • I used your code but I got this message: ( ! ) Warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in C:\wamp\www\index.php on line 128 – Blue Sky Jan 22 '14 at 19:30
  • Then you want to change your php.ini file to accept SSL. See: http://stackoverflow.com/questions/5444249/unable-to-find-the-wrapper-https-did-you-forget-to-enable-it-when-you-config –  Jan 22 '14 at 19:43
  • I did that but now I get this message: Warning: file_get_contents(https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in C:\wamp\www\index.php on line 128 – Blue Sky Jan 23 '14 at 06:26
  • That's not an issue with your code; it's an issue with Google. Their APIs have request limits and may fail if the parameters are rejected. See: https://developers.google.com/maps/documentation/business/articles/usage_limits#http403 –  Jan 23 '14 at 21:11
0

If it's a simple GET request, file_get_contents can do that reasonably well :

$return = file_get_contents('https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29');

Provided you can process the return body (depends on content-type), and do not need to process errors/special http headers/authentication.

Calimero
  • 4,238
  • 1
  • 23
  • 34
0

Use "file_get_contents" with the right configuration.

http://fr.php.net/file_get_contents

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
  • I tried this but I got this warning/error: Warning: file_get_contents(https://www.googleapis.com/freebase/v1/search?indent=true&filter=%28any+type%3A%2Fcvg%2Fcomputer_videogame%29): failed to open stream: Result too large – Blue Sky Jan 22 '14 at 19:28
  • 2
    Take a look at the server configuration. – David Ansermot Jan 22 '14 at 22:43