简体   繁体   English

PHP curl响应因网络而异(编码问题?)

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

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. 我有一个PHP / JS页面,该页面使JQuery ajax调用另一个PHP脚本,后者调用REST服务并将响应发送回PHP / JS页面。 I did this because I couldn't find a non JSONP way to call the service from JS (different domain). 我这样做是因为我找不到从JS(不同域)调用服务的非JSONP方式。

Anyways, from home it works perfectly. 无论如何,在家中都能完美工作。 I deploy at the office and first got apache errors like this: 我在办公室部署,首先遇到类似以下的Apache错误:

Problem (2) in the Chunked-Encoded data 块编码数据中的问题(2)

I was able to get around this by adding: CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0 to my curl options. 我可以通过在curl选项中添加:CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0来解决此问题。

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. 我将$ response登录到磁盘,并在JSON之前看到ó,在JSON之后看到Ê。

------------------------------ SECOND EDIT FOLLOWS----------------------------- ------------------------------第二次编辑------------

I apparently needed to change to this: $request_headers[] = 'application/x-www-form-urlencoded'; 我显然需要更改为:$ request_headers [] ='application / x-www-form-urlencoded';

Thanks for the tips. 感谢您的提示。

I found it from some other stack question here , and it solved my issue as well, it looks like curl version issue. 我从这里的其他堆栈问题中找到了它,它也解决了我的问题,看起来像curl版本的问题。

Add this option into your curl handler: 将此选项添加到您的curl处理程序中:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM