Home > Backend Development > PHP Tutorial > PHP interface--the solution to incomplete messages received due to curl request

PHP interface--the solution to incomplete messages received due to curl request

little bottle
Release: 2023-04-06 07:16:01
forward
3161 people have browsed it

The main content of this article is about PHP interface issues. When the encoding of the header is inconsistent with the encoding of the body message, the curl request causes the received message to be incomplete. Interested friends can learn more.

Question:

For example, during a certain docking process, the header returned by the other party is gbk encoded (Content-type: text/xml; charset=GBK), but the message in the body It is UTF-8 encoding, causing the returned message to be incomplete

Detailed description:

The specific reason is that because of GBK encoding, one Chinese character occupies two bytes, and UTF-8 encoding One Chinese character occupies three bytes. The character length returned by the curl request response is calculated according to GBK encoding. As a result, the calculated character length of the message is smaller than the actual length. Therefore, when reading the response data, the calculated character length is calculated. It will stop when the length of the message is reached, and the returned message will be incomplete.

Solution:

Do not use the curl method to make requests, use the file_get_contents method instead, because file_get_contents will not return head information, so there will be no coding inconsistencies

Related tutorials:PHP video tutorial

The above is the detailed content of PHP interface--the solution to incomplete messages received due to curl request. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template