5

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.

3
  • At which point does the encoding appear to get different? The REST, the Ajax or the Browser request? Make sure to use the same encoding, prefered UTF-8 everywhere. Commented Sep 12, 2013 at 14:30
  • When you say it doesn't have the same result from work as home, are you moving the code to a different server or connecting to the same server but just from different locations? If the server-side is consistent, then you should confirm if the issue occurs across different browsers and other client-side factors. Also, if you can retrieve the raw response, you should compare what is being sent in both scenarios. Commented Sep 12, 2013 at 14:30
  • Regarding the point when I hit encoding problems, I need to log what I get back from the service to disk to be sure. I know that what I send is good, because I get a response, and I know it has issues by the time it arrives at the page that made the ajax request. I will update the question once I have checked. Commented Sep 12, 2013 at 14:45

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.