Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP curl returning strange characters

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

like image 256
user3333047 Avatar asked Feb 20 '14 14:02

user3333047


Video Answer


1 Answers

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.

like image 153
Sabuj Hassan Avatar answered Oct 15 '22 06:10

Sabuj Hassan