Question

I am retreiving json data from cloudmade api using php, and using json_decode to parse it. however it returns NULL. Any ideas as to why this is happening will be helpful. My code is shown below:

$url = 'http://routes.cloudmade.com/81aa79a9504e4430a8a32f491ef96f07/api/0.3/'.$curr_x.','.$curr_y.','.$dest_x.','.$dest_y.'/car.js';
    $aContext = array(
        'http' => array(
            'proxy' => 'tcp://10.3.100.212:8080',
            'request_fulluri' => true,
            'header' => 'Accept-Charset: UTF-8, *;q=0'
        ),
    );
    $cxContext = stream_context_create($aContext);

    $sFile = file_get_contents($url, False, $cxContext);
    //echo $sFile;
    $obj = json_decode($sfILE);
    print $obj;
    print $obj->{'total_time'};

Below is a sample json data I am retrieving:

{"version":0.3,"status":0,"route_summary":{"total_distance":280574,"total_time":11505,"start_point":"Hwy2","end_point":"Hwy14"},"route_geometry":[[25.28581,121.602463],[25.28508,121.603691],[25.28401,121.604752],[25.28265,121.605553],[25.28133,121.6064],[25.281059,121.606613],[25.280451,121.607819],[25.280149,121.608543],[25.27976,121.610397],[25.279289,121.611794],[25.279119,121.61248],[25.2791,121.613541],[25.278839,121.614532],[25.278351,121.615669],[25.2777,121.616783],[25.27721,121.617447],[25.27286,121.622032],[25.26898,121.62632],[25.2658,121.628899],[25.263941,121.63031],[25.26215,121.631302],[25.259251,121.632896],[25.258341,121.63324],[25.25765,121.63327],[25.257299,121.633324],[24.005039,121.059502]],"route_instructions": [["Head southeast on Hwy2",479,0,23,"0.5 km","SE",123.2],["Continue on Hwy2",205,3,10,"0.2 km","SE",149.6,"C",357.8],["Slight left at Hwy2",12918,5,620,"12.9 km","SE",119.3,"TSLL",335.3],["Continue on Hwy2",1248,96,60,"1.2 km","E",107.9,"C",359.9],["Continue on Hwy2",4221,109,203,"4.2 km","E",86.6,"C",0.0],["Continue",221,145,11,"0.2 km","S",164.6,"C",12.8],["Slight right",749,150,36,"0.7 km","SE",151.9,"TSLR",9.3],["Slight left",5755,160,276,"5.8 km","S",186.8,"TSLL",333.1],["Slight left at Hwy2",1840,269,88,"1.8 km","SE",124.5,"TSLL",326.7],["Turn right at Fwy3\/ç¦��ç��¾æ��©æ²�é«��é�顫å��¬è·¯ (Formosa Freeway)",2601,305,117,"2.6 km","S",159.0,"TR",68.6],["Continue on Fwy3\/ç¦��ç��¾æ��©æ²�é«��é�顫å��¬è·¯ (Formosa Freeway)",8484,328,339,"8.5 km","S",196.8,"C",355.8],["Slight right",2454,404,110,"2.5 km","SW",206.6,"TSLR",9.0],["Slight right at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",8802,428,352,"8.8 km","W",266.5,"TSLR",8.2],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",16984,455,679,"17.0 km","W",290.9,"C",0.5],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",2129,556,85,"2.1 km","W",251.5,"C",0.0],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",6560,567,262,"6.6 km","W",258.6,"TSLL",354.7],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",651,636,26,"0.7 km","W",261.6,"C",0.2],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",8269,639,331,"8.3 km","W",267.7,"C",1.9],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",2914,691,117,"2.9 km","SW",239.6,"C",0.0],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",8227,699,329,"8.2 km","SW",225.0,"TSLL",358.2],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",6311,706,252,"6.3 km","SW",222.4,"C",0.8],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",2433,722,97,"2.4 km","SW",219.7,"TSLL",353.9],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",3964,736,159,"4.0 km","SW",205.2,"C",0.1],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",27836,751,1113,"27.8 km","SW",224.0,"TSLL",354.4],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",3188,830,128,"3.2 km","SW",210.0,"C",3.8],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",5626,839,225,"5.6 km","S",188.3,"C",359.8],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",729,864,29,"0.7 km","SW",244.9,"TSLL",353.4],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",20400,870,816,"20.4 km","SW",213.2,"C",0.2],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",57912,948,2316,"57.9 km","SW",206.9,"C",355.8],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",5941,1409,238,"5.9 km","S",186.9,"C",0.3],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",4580,1439,183,"4.6 km","SW",243.5,"C",359.1],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",4743,1461,190,"4.7 km","SW",206.7,"C",2.7],["Continue on Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",7445,1482,298,"7.4 km","S",184.7,"C",358.7],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",1471,1526,59,"1.5 km","W",289.1,"TSLL",355.6],["Slight left at Fwy1\/中山é«��é�顫å��¬è·¯ (Jhongshan Freeway)",3024,1535,121,"3.0 km","W",258.5,"TSLL",353.7],["Slight right",1157,1551,52,"1.2 km","SW",232.6,"TSLR",8.4],["Slight right",4609,1570,207,"4.6 km","NE",26.5,"TSLR",29.9],["Slight left at Fwy3\/ç¦��ç��¾æ��©æ²�é«��é�顫å��¬è·¯ (Formosa Freeway)",8075,1609,323,"8.1 km","E",102.0,"TSLL",358.4],["Slight left at Fwy3\/ç¦��ç��¾æ��©æ²�é«��é�顫å��¬è·¯ (Formosa Freeway)",4611,1680,184,"4.6 km","SE",116.5,"TSLL",354.5],["Continue on Fwy3\/ç¦��ç��¾æ��©æ²�é«��é�顫å��¬è·¯ (Formosa Freeway)",2669,1708,107,"2.7 km","S",160.6,"C",3.9],["Slight right",22697,1719,1021,"22.7 km","SW",232.5,"TSLR",18.0],["Continue on Fwy6",14915,1788,597,"14.9 km","E",77.9,"C",9.6],["Continue on Fwy6",11333,1815,453,"11.3 km","E",73.7,"C",351.8],["Turn left at Hwy14",8019,1849,385,"8.0 km","E",86.5,"TL",261.1]]}
Was it helpful?

Solution

Change $sfILE to $sFile

$url = 'http://routes.cloudmade.com/81aa79a9504e4430a8a32f491ef96f07/api/0.3/'.$curr_x.','.$curr_y.','.$dest_x.','.$dest_y.'/car.js';
    $aContext = array(
        'http' => array(
            'proxy' => 'tcp://10.3.100.212:8080',
            'request_fulluri' => true,
            'header' => 'Accept-Charset: UTF-8, *;q=0'
        ),
    );
    $cxContext = stream_context_create($aContext);

    $sFile = file_get_contents($url, False, $cxContext);
    //echo $sFile;
    $obj = json_decode($sFile);
    print $obj;
    print $obj->{'total_time'};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top