problem description
the encoding of the head header is inconsistent with that of the body message, and the curl request causes the received message to be incomplete
the environmental background in which the problem occurs
For example, in a docking process, the header returned by the other party is gbk-encoded (Content-type: text/xml; charset=GBK), but the message in body is UTF-8-encoded, resulting in incomplete messagesrelated codes
function curl($url,$data = "",$header=null)
{
//curl
$curl = curl_init();
//cURL
if(is_array($header))
{
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){//post
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
//
$output = curl_exec($curl);
$error = curl_errno($curl);
//cURL
curl_close($curl);
if($error){
return $error;
}else{
return $output;
}
}
detailed description
The specific reason is because of GBK encoding, a Chinese character takes up two bytes, while a Chinese character encoded by UTF-8 takes up three bytes. The character length returned by the curl request response is calculated according to the GBK code, so the calculated message character length is less than the actual length, so when reading the response data, it stops reading the calculated length. The returned message is incomplete .solution:
instead of using the curl method to make the request, we use the file_get_contents method instead. Because file_get_contents does not return head information, there will be no coding inconsistencies
destroy requests
invalid requests now I would like to ask the gods, is there any parameter that can be set by curl method to solve this problem?