11

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

user3333047
  • 111
  • 1
  • 1
  • 3
  • Try changing that line to: `curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=UTF-8'));`. – Amal Murali Feb 20 '14 at 14:27

1 Answers1

36

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.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • Setting curl_setopt($ch, CURLOPT_ENCODING, ''); fixed a very strange encoding issue I had where the curl output looked like it was inserting extra characters which was invalidating the XML retrieved. – Baxny May 29 '14 at 01:13
  • Super thank you for the answer. It works for me. But ... What the heck? – bharatesh Feb 02 '16 at 15:44
  • You gave us the answer. Can you please also mention why such errors occur & how we can find the cause ? " the compressed content which your curl failed to detect" What does this mean ? Can you please explain in a little more detail ? – MasterJoe Aug 03 '17 at 17:42