您好,欢迎访问一九零五行业门户网

PHP基于curl post实现发送url中文乱码怎么办

php基于curl post实现发送url中文乱码的解决办法:首先确认网址编码;然后使用curl需要加header设置charset,代码为【$this_header = array(“charset=utf-8”)】。
php基于curl post实现发送url中文乱码的解决办法:
发送的指定网址的url参数,中文总是乱码,指定网址是utf8编码的,我发送的也是utf8编码的。但是还是乱码,开始用的file_get_contents,后来换成curl并在php.ini中开启了php_curl,还是不行,又加了header终于解决。代码如下:
$url = 'http://'; //调用接口的平台服务地址$post_string = array('a'=>'b');$ch = curl_init();$this_header = array("content-type: application/x-www-form-urlencoded; charset=utf-8");curl_setopt($ch,curlopt_httpheader,$this_header);curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_postfields, $post_string);curl_setopt($ch, curlopt_returntransfer, true);curl_setopt($ch, curlopt_connecttimeout, 10);curl_setopt($ch, curlopt_timeout, 30);$result = curl_exec($ch);if($result)echo "<script>\nalert(\"同步成功! \");\n</script>";curl_close($ch);
总结:解决此类编码问题,首先,要确认两个地方的编码是什么,其次,如果编码相同那么可以直接发送,使用curl需要加header设置charset,最后,多查多试,一种方法不行再换另一个试试,如果都不行,那么就从头再考虑一遍问题,总能解决的。
相关学习推荐:php编程(视频)
以上就是php基于curl post实现发送url中文乱码怎么办的详细内容。
其它类似信息

推荐信息