Home  >  Article  >  Backend Development  >  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
little bottleforward
2019-04-20 11:39:022966browse

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete