Baadier Sydow
Baadier Sydow

Reputation: 504

How to load externally hosted data in JSON format

What would the syntax be to pull an external data source, which has the data in JSON format into a variable to be worked with. I understand using json_decode($variable) but how would i load the actual data into that variable for decoding?

Upvotes: 5

Views: 6983

Answers (4)

Slavic
Slavic

Reputation: 1962

Use anything from fopen + fread to php curl library. With fopen you could open a remote file if php settings allows you to. I think you should be able to do it now. If you still can't do it, let us know.

Upvotes: 0

methode
methode

Reputation: 5438

If by external you mean that it's hosted on a 3rd-party domain name, then you open a socket and GET the data:

$variable = file_get_contents('http://example.com/data.json');
$decoded = json_decode($variable);

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799240

With fopen(), fread(), and fclose(), or with file_get_contents().

Upvotes: 1

MatTheCat
MatTheCat

Reputation: 18761

Using file_get_contents() ? (You must have allow_url_fopen true)

Upvotes: 0

Related Questions