PHP is based on curl post to implement a solution to sending Chinese garbled URLs: first confirm the encoding in two places; then if the encoding is the same, then you can send it directly. When using curl, you need to add a header to set the charset. The code is [ $this_header = array].
PHP is based on curl post to implement the solution for sending Chinese garbled URLs:
The url parameters of the specified URL sent , Chinese characters are always garbled, the specified URL is utf8 encoded, and what I sent is also utf8 encoded. But the code is still garbled. I used file_get_contents
at first, then changed it to curl
and opened php_curl
in php.ini, but it still didn’t work, so I added header
is finally solved. The code is as follows:
$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 type of coding problem, first, confirm what the coding is in the two places. Secondly, if the coding is the same, you can directly To send, you need to add the header setting charset
when using curl. Finally, check more 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. It can always be solved. .
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of What should I do if PHP implements sending Chinese garbled URLs based on curl post?. For more information, please follow other related articles on the PHP Chinese website!