无法通过网址从URL解码json

I am trying to read json vi php but unable to decode

<?php 
$json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json";
$json = file_get_contents($json_url);

$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";

?>

If you visit your target URL you will notice the JSON value is wrapped in a callback method which you could trim as by something like

$json = str_replace('finance_charts_json_callback(', '', substr($pageContent, 0, strlen($pageContent) - 1));

Here is how it looks all put together:

<?php 
$json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json"; $pageContent = file_get_contents($json_url);

$json = str_replace('finance_charts_json_callback(', '', substr($pageContent, 0, strlen($pageContent) - 1));

$data = json_decode($json, TRUE);

echo "<pre>"; print_r($data); echo "</pre>";

One thing you can do is work with it in JavaScript:

<script>
function finance_charts_json_callback(json){
    console.log(json.meta);
}
</script>

<?php 
$json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json";
$data = file_get_contents($json_url);

echo "<script>";
print_r($data);
echo "</script>";
?>

Other thing is try to remove the 'function' part:

<?php 
$json_url = "http://chartapi.finance.yahoo.com/instrument/1.0/AAPL/chartdata;type=quote;range=5d/json";
$json = file_get_contents($json_url);
$json = substr($json, 30);
$json = substr($json, 0, strlen($json)-2);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";

?>

The response from this service is not a valid JSON object its JSONP, you should look for a service that provides output as JSON.

In case they don't provide a service like that you can manipulate the data before decoding it using this workaround

$data = str_replace('finance_charts_json_callback( ', '', $data);
$data = str_replace(' )', '', $data);