0

I have an extremely simple script:

<?php
$jsonurl = "http://api.wipmania.com/json";
$json = file_get_contents($jsonurl);
echo $json;
?>

It works for this URL, but when I call it with this URL: https://erikberg.com/nba/standings.json

it is not echoing the data. What is the reason for this? I'm probably missing a concept here. Thanks

1
  • you have ssl configured properly for this on your server? Commented Feb 16, 2015 at 2:13

1 Answer 1

2

The problem for that particular URL is that it's expecting a different User Agent, different to the default that PHP is using with file_get_contents()

Here is a better example using CURL. It's more robust although it takes more lines of code to configure it and make it run:

// create curl resource
$ch = curl_init();

// set the URL
curl_setopt($ch, CURLOPT_URL, 'https://erikberg.com/nba/standings.json');

// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Fake the User Agent for this particular API endpoint
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

// $output contains the output string.
$output = curl_exec($ch);

// close curl resource to free up system resources.
curl_close($ch);

// You have your JSON response here
echo $output;
Sign up to request clarification or add additional context in comments.

3 Comments

Any idea on why this runs so slow?
From my side it does not run slow at all. That request takes 1.25 secs aprox. Looks like pretty normal response time.
Ah gotchya, must be something on my end then, it's taking 15+ seconds for me. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.