0

I'm trying to get a JSON string from a page in my Laravel Project. Using this:

$json = file_get_contents($url);
$data = json_decode($json, TRUE);
return View::make('adventuretime.marceline')
    ->with('json', $json)
    ->with('title', 'ICE KING')
    ->with('description', 'I am the Ice King')
    ->with('content', 'ice king');

But since I'm only using a localhost, I think this doesn't work that's why it doesn't output anything. I want to know what is the proper way for it to be flexible and be able to get the JSON string with any $url value using php?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
OneScrewLoose
  • 145
  • 1
  • 6
  • 22
  • possible duplicate of [PHP ini file\_get\_contents external url](http://stackoverflow.com/questions/3488425/php-ini-file-get-contents-external-url) – NDM Sep 06 '13 at 08:57
  • that it's a localhost doesn't matter at all. That works exactly the same as a 'normal' host. Can you share the output of the file_get_contents() call? (`print_r($json)` or something like that)? – giorgio Sep 06 '13 at 08:58
  • try to set `error_reporting(E_ALL);` – Shushant Sep 06 '13 at 08:59
  • @giorgio is right. Can you post the value of `$url` as well? – insertusernamehere Sep 06 '13 at 08:59
  • 'http://localhost:8000/finn', since I'm using Laravel I tried using 'finn' only – OneScrewLoose Sep 06 '13 at 09:03
  • I get an error `failed to open stream: No such file or directory`, and some times the page continues to load but is not actually responding – OneScrewLoose Sep 06 '13 at 09:06
  • You have to use `http://localhost:8000/[…]`. Does the URL work in your browser directly? – insertusernamehere Sep 06 '13 at 09:17
  • Actually I've tried using both but they're not the answer – OneScrewLoose Sep 06 '13 at 09:18
  • Well the error immediately is your answer, isn't it? The page (endpoint of your url) cannot be found... First try to reach the url in the browser, you should see some plain json output. If that works, put the same url in your `$url` parameter and you're just fine. – giorgio Sep 06 '13 at 15:10

1 Answers1

0

Looking at the comments above, it is possible that the $url you are using is not valid, check it out by pointing your browser there and see what happens.

If you are sure that the $url is fine, but you still get the 404 Not Found error - verify that you have proper Laravel routing defined for that address. If the routes are fine, maybe you forgot to do

composer dump-autoload 

after making modifications in your routes.php. If so, try the above and refresh the browser to see if it helps.


Furthermore, bear in mind that using your current function, you can submit only GET requests. What is more, this function might not be available for fetching remote urls, on some hosting servers due to security reasons. If you still want to use it, it'd be good to check

if($json !== FALSE)

before you process the $json response. If the file_get_contents fails it will return false.

Reffering to the part of your question

what is the proper way for it to be flexible and be able to get the JSON string with any $url

I'd suggest using cURL, as a standard and convenient way to fetch remote content. Using cURL you have better control over the process of sending the http request and receiving the "answer" it returns. Personaly, in my Laravel 4 apps I often use this package jyggen/curl. You can read the docs for it here: jyggen docs

If you are not satisfied with cURL and you want greater control try Guzzle As the authors state, Guzzle is a PHP HTTP client & framework for building RESTful web service clients.

Gadoma
  • 6,475
  • 1
  • 31
  • 34