Home  >  Article  >  Backend Development  >  What should I do if PHP implements sending Chinese garbled URLs based on curl post?

What should I do if PHP implements sending Chinese garbled URLs based on curl post?

coldplay.xixi
coldplay.xixiOriginal
2020-08-06 09:45:033805browse

PHP is based on curl post to implement the solution to send Chinese garbled URLs: first confirm the URL encoding; then use curl to add a header to set the charset, the code is [$this_header = array ("charset=UTF-8")] .

What should I do if PHP implements sending Chinese garbled URLs based on curl post?

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 to curl and turned on php_curl in php.ini. It still didn't work. I added header and finally solved it. 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 encoding problem, first, 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 a header to set the charset. Finally, Check more and try more. If one method doesn't work, try another. If it doesn't work, then think about the problem again from the beginning. You can always solve it.

Related learning recommendations: php programming (video)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn