function posturl($gateurl,$data){
$headers = array("Content-type: application/x-www-form-urlencoded", "'charset='utf-8'");
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$gateurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch ,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch ,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$res = curl_exec($ch);
curl_close($ch);
$result = json_decode($res, true);
return $result;
}
上面的代码块http_build_query:使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串,
当传送的value值里面有json_encode时,里面会被http_build_query转成实体,需要再转回来再json_decode;
本文主要为大家讲解了php在使用json_decode函数解码json字符串时,解码不成功返回NULL的问题缘由分析和解决方法,感兴趣的同学参考下.
一般来说,php对json字符串解码使用json_decode()函数,第一个参数传字符串,第二个参数若为true,返回array;若为false,返回object。如果返回NULL,说明报错,输出json_last_error(),得到的整数值对应错误提示。如下图所示:

json_last_error()比较常见的是整数4, 是json字符串在json_decode之前已不完整,
用下面的方法可以实现正确解析
$data = stripslashes(html_entity_decode($info)); //$info是传递过来的json字符串
$data = json_decode($data,TRUE);
这时候$data就是解析后的数组了!
html_entity_decode() 函数的作用是把 HTML 实体转换为字符。
stripslashes() 函数的作用是删除反斜杠。
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END



![nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf - 鹿快](https://img.lukuai.com/blogimg/20251024/d9fe824b72624ed788b1dd01dc0e7f68.jpg)










暂无评论内容