0

我正在尝试从以下 url 获取 json 数据:

https://chart.googleapis.com/chart?cht=gv&chl=digraph框架 { 1[color=red]; 2;3[颜色=红色];4;5个;6;7; 8个;9; 10个;11; 12[颜色=红色];13; 14[颜色=红色];15; 16[颜色=红色];17[颜色=红色];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

我尝试了其他问题中建议的几种方法:

$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 an error from the google page: 400. That’s an error. Your client has issued a malformed or illegal request. That’s all we 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
 // This makes sense if the JSON isn't not returned correctly. I think json_decode() returns NULL if the input is not correct json data.

 //Method 3
 $json = file_get_contents(urlencode($jsonurl));
 // Gives another error: [function.file-get-contents]: failed to open stream: File name too long

如果我只是在我的代码中发布原始 JSON 数据,那么我的算法的其余部分就可以正常工作。关键是,在我的实际代码中,url 是动态的,我需要能够从 url 获取 JSON 数据。

这似乎是谷歌的安全问题。但是,我不知道如何解决这个问题。

这是我在 Stackoverflow 上的第一个问题。如果您想了解更多/我是否应该以某种方式编辑我的问题,请告诉我。

提前致谢!

4

1 回答 1

0

您的 URL 需要进行 URI 转义,以便 cURL 正确使用它。如果我将您的脚本更改为:

# note that in this string, all special characters (eg. spaces) are %-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";

# your settings here are fine
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, $jsonurl);
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

# XXX: not entirely safe here
# curl_exec() could dump out something other than JSON if it encounters an error
# I recommend saving the output of curl_exec($c) separately in case of error
$json = json_decode(curl_exec($c));

然后$json包含我期望的数据对象。我在 Mac 上的 PHP 5.3.15 中对此进行了测试。

于 2013-02-11T02:37:25.813 回答