function sendpost($url,$postdata){
$postdata = http_build_query($postdata);
$options = array(
'http' => array(
'method' => 'post',
'header' => 'content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
$url = http://chepiao.sinaapp.com/api.php?act=remain;
$data = array(
date=>20131220,
startstation=>北京,
arrivestation=>天津
);
$result = sendpost($url, $data);
$obj = json_decode($result);
$result就是返回来的数据,就是解析不了,用json_decode($result);解析成的是null
分享到: 更多
------解决方案--------------------
本帖最后由 xuzuning 于 2013-12-17 19:19:36 编辑 连csdn都不能接受这种字符集的错乱
utf-8环境看到
------解决方案--------------------
真奇葩
echo json_last_error();
结果是4,说明格式不正确
于是把返回的东西扔http://jsonlint.com/检测,报错,发现是换行的问题
print_r(json_decode(str_replace(\n, ,$result)));
结果就好了。