Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP curl response differs based on network (encoding issue?)

Tags:

php

curl

I have a PHP/JS page that makes JQuery ajax calls to another PHP script which calls out to a REST service and sends the response back the PHP/JS page. I did this because I couldn't find a non JSONP way to call the service from JS (different domain).

Anyways, from home it works perfectly. I deploy at the office and first got apache errors like this:

Problem (2) in the Chunked-Encoded data

I was able to get around this by adding: CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0 to my curl options.

However, the data that is coming through now has a question mark in a diamond, chars at the beginning and end that don't belong, etc. From what I have found this could be an encoding problem, but my efforts to fix it did not work.

Again, works perfectly from home... not from work.

Any help greatly appreciated

------------------------------ EDIT FOLLOWS-----------------------------

The encoding issue first shows up in the response back from the service. This is the code for the send/receive:

            $request_headers    = array();
            $request_headers[]  = 'Content-Type: application/json';
            $request_headers[]  = 'Authorization: Bearer ' . $token;

            $options = array(
                CURLOPT_URL            => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HEADER         => false,
                CURLOPT_VERBOSE        => true,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_ENCODING       => '',
                CURLOPT_AUTOREFERER    => true,
                CURLOPT_CONNECTTIMEOUT => 120,
                CURLOPT_TIMEOUT        => 120,
                CURLOPT_MAXREDIRS      => 10,
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_HTTPHEADER     => $request_headers,
                    CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_0
            );

            curl_setopt_array( $ch, $options );
            $response = curl_exec($ch); 

I log $response to disk and see a ó before the JSON and a Ê after it.

------------------------------ SECOND EDIT FOLLOWS-----------------------------

I apparently needed to change to this: $request_headers[] = 'application/x-www-form-urlencoded';

Thanks for the tips.

like image 623
chrismead Avatar asked Sep 12 '13 14:09

chrismead


1 Answers

I found it from some other stack question here, and it solved my issue as well, it looks like curl version issue.

Add this option into your curl handler:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
like image 105
KKK Avatar answered Sep 22 '22 02:09

KKK