Home >Backend Development >PHP Tutorial >How to deal with Chinese garbled characters when PHP sends URL based on curl post
This problem often troubles everyone. The Chinese characters of the url parameters of the specified URL sent are always garbled, and the specified URL is encoded in utf8. Today we will completely solve this problem for everyone. code show as below.
$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);
Summary: To solve this kind of encoding problem, firstly, confirm what the encoding is in the two places. Secondly, if the encoding is the same, it can be sent directly. When using curl, you need to add Set the charset in the header. Finally, check and try more. If one method doesn't work, try another one. If it doesn't work, then think about the problem again from the beginning, and you can always solve it.
PS: Here content-type is set to: application/x-www-form-urlencoded;
Recommended reading:
PHP CURL post error failed creating formpost data solution
CURL POST data volume Too large, unable to receive information from the server
The above is the detailed content of How to deal with Chinese garbled characters when PHP sends URL based on curl post. For more information, please follow other related articles on the PHP Chinese website!