FabianW
FabianW

Reputation: 335

file_get_contents request to external site

I am trying to do a file_get_contents of this Demo URL
However the server has trouble with getting data from external sites. This is the error I get if I echo the file_get_contents:

Found The document has moved here. Apache/2.4 Server at spotifycharts.com Port 80

I have turned the register_global on in the php.ini file, but this doesn't help.

What would be the most logical thing to check to make sure my website is able to get data from external sites?

Upvotes: 1

Views: 894

Answers (2)

Philipp
Philipp

Reputation: 15629

Just use the https url instead of the http url:

https://spotifycharts.com/api/?type=regional&country=nl&recurrence=daily&date=latest&limit=200

Upvotes: 3

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

You may need to request with cURL, I don't think file_get_contents() can follow 302 redirects.

Something like this should work...

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if (preg_match('#Location: (.*)#', $a, $r))
  $l = trim($r[1]);

How to get the real URL after file_get_contents if redirection happens?
Source

Upvotes: 0

Related Questions