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?
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);
With fopen()
, fread()
, and fclose()
, or with file_get_contents()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With