从URL获取JSON

时间:2013-05-15 18:01:17

标签: php json curl

我正在尝试获取一个JSON字符串并且它不断出现NULL并且我不确定为什么。有人可以查看我的代码是否正确吗?

$url = "http:/engin.umich.edu/college/about/cal/event-calendar/futurejson";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
     $feed = curl_exec($ch);
     curl_close($ch);

    $data = json_decode($feed,true);

1 个答案:

答案 0 :(得分:3)

 $url = "http://engin.umich.edu/college/about/cal/event-calendar/futurejson";
 // missing /  ^
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
 // It's returning a redirect to www.engin.umich.edu
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 $feed = curl_exec($ch);
 curl_close($ch);

 $data = json_decode($feed,true);