Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load externally hosted data in JSON format

Tags:

php

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?

like image 300
Baadier Sydow Avatar asked Oct 07 '10 08:10

Baadier Sydow


2 Answers

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);
like image 151
methode Avatar answered Nov 14 '22 21:11

methode


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

like image 36
Ignacio Vazquez-Abrams Avatar answered Nov 14 '22 23:11

Ignacio Vazquez-Abrams