1

I'm trying to pull a json file of products from a URL in PHP. I have the following code but it's not working. Can somebody please advise where I've gone wrong?

function getJson() {
$request = file_get_contents('https://3rdParty.guntrader.uk/ShootingSuppliesLtd/jsonGuns');
$json = json_decode($request);
return $json;
};

echo $json;

Undefined variable: json in C:\Users\darry\dev\VVV\www\ssheadless\importJson.php on line 36

Ivar
  • 6,138
  • 12
  • 49
  • 61

1 Answers1

2

You didn't call the method getJson(). Without calling a method the return value can't be accessed. Call the method.

function getJson() {
    $request = file_get_contents('https://3rdParty.guntrader.uk/ShootingSuppliesLtd/jsonGuns');
    $json = json_decode($request);
    return $json;
};

echo getJson();
MH2K9
  • 11,951
  • 7
  • 32
  • 49