PHP curl返回奇怪的字符 [英] PHP curl returning strange characters
问题描述
我正在尝试使用Prestashop实例的图像产品更新页面。
I am trying to update a page with image products of a Prestashop instance.
我正在使用prestashop Web服务获取信息。
问题是当我加载页面时,它询问我prestashop的令牌/密钥,但是我想使用Url和我通过CURL传递的密钥来保存登录会话,而不输入每个密钥时间。
但是 curl_exec
的输出是一些奇怪的字符,例如 #B R $ 3br。
I am getting the information using prestashop web services.
The problem is when I load the page , it asks me the Token/key of the prestashop but I would want to save the login session using the Url and the key which I pass from by CURL and not entering the key each time.
However the output of curl_exec
is some strange characters like ��#B��R��$3br�
以下是保存会话的功能:
Here is the function to save the session:
function saveSession($url, $key){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
我不知道问题出在编码,标题还是还有其他解决方案!?
I don't know if the problem is with the encoding, header or is there any other solution!?
推荐答案
响应中那些奇怪的数据是您的curl无法检测到的压缩内容。
Those strange data on response are the compressed content which your curl failed to detect.
替换为:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch');
使用:
curl_setopt($ch, CURLOPT_ENCODING, '');
空编码意味着可以处理任何类型的编码。它应该可以解决您的问题。
Empty encoding means to handle any type of encoding. It should solve your issue.
这篇关于PHP curl返回奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!