PHP curl returning strange characters

user3333047 picture user3333047 · Feb 20, 2014 · Viewed 9.5k times · Source

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

Answer

Sabuj Hassan picture Sabuj Hassan · Feb 20, 2014

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.