0

Hey so I'm having a slight dilemma with getting the contents of a file using a variable.

So to explain the code below a little, the respform fetches JSON array all ok. And the results url when echo'd displays like a normal URL that when viewed displays JSON data. Then I want to fetch JSON data from the second URL. If I use this variable in file_get_contents nothing happens. If I simply create a variable $url = '' and type the same address it works fine.

I've var dumped the $resulturl variable that I'm using and it is a string(56). I've tried using json_encode and it becomes a string(64). What sort of data does it need to be to be accepted into the file_get_contents.

$resp = file_get_contents($url, FALSE, $context);
$respform = json_decode($resp, TRUE);
$resulturl = $respform['resultsUrl'];

$data = file_get_contents($resulturl, FALSE);
$insta_array = json_decode($data, TRUE);
print_r($insta_array);

Hope someone can help, Thanks!

Anthony
  • 36,459
  • 25
  • 97
  • 163
Jack Ellis
  • 29
  • 1
  • 1
  • 6

1 Answers1

0

$resulturl apparently contains a JSON-encoded URL. You need to do:

$resulturl = json_decode($respform['resultsUrl']);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I just tried this but I var_dumped the value of resulturl just after and it returned a value of "NULL" – Jack Ellis Apr 06 '18 at 23:52
  • Then go one step backward (as many times as needed) until you find a successful/expected outcome, then code forward from that point. Please show us (in an edit to your question) all of the check points in this task. Echo and var_export after every variable that you declare. – mickmackusa Apr 06 '18 at 23:57
  • @JackEllis The `var_dump()` output you showed in the comment really looks like JSON, so I'm not sure why this didn't work. – Barmar Apr 06 '18 at 23:58