Tuesday, 15 July 2014

php - Get json content from a Google Chart url -



php - Get json content from a Google Chart url -

i'm trying json info next url:

https://chart.googleapis.com/chart?cht=gv&chl=digraph framework { 1[color=red]; 2; 3[color=red]; 4; 5; 6; 7; 8; 9; 10; 11; 12[color=red]; 13; 14[color=red]; 15; 16[color=red]; 17[color=red]; 2 -> 1; 3 -> 1; 4 -> 1; 5 -> 1; 6 -> 2; 8 -> 9; 9 -> 8; 12 -> 2; 12 -> 3; 12 -> 4; 12 -> 5; 7 -> 6; 7 -> 10; 7 -> 11; 8 -> 7; 13 -> 5; 14 -> 13; 15 -> 6; 16 -> 15; 17 -> 12; 10 -> 4; 11 -> 3; 14 -> 16; 16 -> 17; }&chof=json

i have tried several methods suggested in other questions:

$jsonurl = "https://chart.googleapis.com/chart?cht=gv&chl=digraph framework { 1[color=red]; 2; 3[color=red]; 4; 5; 6; 7; 8; 9; 10; 11; 12[color=red]; 13; 14[color=red]; 15; 16[color=red]; 17[color=red]; 2 -> 1; 3 -> 1; 4 -> 1; 5 -> 1; 6 -> 2; 8 -> 9; 9 -> 8; 12 -> 2; 12 -> 3; 12 -> 4; 12 -> 5; 7 -> 6; 7 -> 10; 7 -> 11; 8 -> 7; 13 -> 5; 14 -> 13; 15 -> 6; 16 -> 15; 17 -> 12; 10 -> 4; 11 -> 3; 14 -> 16; 16 -> 17; }&chof=json" //method 1 $json = file_get_contents($jsonurl); // returns error google page: 400. that’s error. client has issued malformed or illegal request. that’s know //method 2 $curlsession = curl_init(); curl_setopt($curlsession, curlopt_url, $jsonurl); curl_setopt($curlsession, curlopt_binarytransfer, true); curl_setopt($curlsession, curlopt_returntransfer, true); $json_decoded = json_decode(curl_exec($curlsession)); curl_close($curlsession); // returns null // makes sense if json isn't not returned correctly. think json_decode() returns null if input not right json data. //method 3 $json = file_get_contents(urlencode($jsonurl)); // gives error: [function.file-get-contents]: failed open stream: file name long

if post raw json info in code rest of algorithm works fine. point is, in actual code url dynamic , need able json info url.

it seems security issue google. however, don't have clue of how solve this.

this first question on stackoverflow. please allow me know if want know more/if should edit question somehow.

thanks in advance!

your url needs uri-escaped curl utilize properly. if alter script this:

# note in string, special characters (eg. spaces) %-escaped $jsonurl = "https://chart.googleapis.com/chart?cht=gv&chl=digraph%20framework%20{%201[color=red];%202;%203[color=red];%204;%205;%206;%207;%208;%209;%2010;%2011;%2012[color=red];%2013;%2014[color=red];%2015;%2016[color=red];%2017[color=red];%202%20-%3e%201;%203%20-%3e%201;%204%20-%3e%201;%205%20-%3e%201;%206%20-%3e%202;%208%20-%3e%209;%209%20-%3e%208;%2012%20-%3e%202;%2012%20-%3e%203;%2012%20-%3e%204;%2012%20-%3e%205;%207%20-%3e%206;%207%20-%3e%2010;%207%20-%3e%2011;%208%20-%3e%207;%2013%20-%3e%205;%2014%20-%3e%2013;%2015%20-%3e%206;%2016%20-%3e%2015;%2017%20-%3e%2012;%2010%20-%3e%204;%2011%20-%3e%203;%2014%20-%3e%2016;%2016%20-%3e%2017;%20}&chof=json"; # settings here fine $curlsession = curl_init(); curl_setopt($curlsession, curlopt_url, $jsonurl); curl_setopt($curlsession, curlopt_binarytransfer, true); curl_setopt($curlsession, curlopt_returntransfer, true); # xxx: not exclusively safe here # curl_exec() dump out other json if encounters error # recommend saving output of curl_exec($c) separately in case of error $json = json_decode(curl_exec($c));

then $json contains info object expect. tested in php 5.3.15 on mac.

php json google-visualization

No comments:

Post a Comment