PHP curl returning strange characters

旧城冷巷雨未停 提交于 2019-12-12 07:56:20

问题


I am trying to update a page with image products of a Prestashop instance.

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!?


回答1:


Those strange data on response are the compressed content which your curl failed to detect.

Replace this:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch');

With:

curl_setopt($ch, CURLOPT_ENCODING, '');

Empty encoding means to handle any type of encoding. It should solve your issue.



来源:https://stackoverflow.com/questions/21910607/php-curl-returning-strange-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!