PHP PROXY很难用于“POST”数据,但能够使用GET

时间:2013-06-12 11:16:07

标签: php proxy xmlhttprequest

http://benalman.com/code/projects/php-simple-proxy/examples/simple/

我正在关注以上博客使用跨域的PHP代理设置。我正在使用XHR。我能够成功使用GET方法。但是在使用POST时,我在回复对象中收到错误CODE 200和Empty XML。

然而,当我使用简单的XHR代码没有phpproxy与谷歌的设置低于。 chrome.exe --disable-web-security。我对GET和POST都很成功。

我确信我在XHR.Send(Mydata)的某个地方错了。但是,如果我在这种方法中错了,那么我就无法发送成功的完整帖子方法。

请帮忙。我是PHP的新手我确信我错过了PHP代码中的一些东西,可以让我发布成功。下面是PHP代码的关键。

            $enable_jsonp    = true;
            $enable_native   = false;
            $valid_url_regex = '/.*/';

            $url = $_GET['url'];

            if (!$url) 
            {

                // Passed url not specified.
                $contents = 'ERROR: url not specified';
                $status   = array(
                    'http_code' => 'ERROR'
                );

            } 
            else if (!preg_match($valid_url_regex, $url)) {

                // Passed url doesn't match $valid_url_regex.
                $contents = 'ERROR: invalid url';
                $status   = array(
                    'http_code' => 'ERROR'
                );

            } 
            else 
            {
                $ch = curl_init($url);
                if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') 
                {
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
                }

                if ($_GET['send_cookies']) 
                {
                    $cookie = array();
                    foreach ($_COOKIE as $key => $value) 
                    {
                        $cookie[] = $key . '=' . $value;
                    }
                    if ($_GET['send_session']) 
                    {
                        $cookie[] = SID;
                    }
                    $cookie = implode('; ', $cookie);
                    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
                }

                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT']);
                list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', curl_exec($ch), 2);
                $status = curl_getinfo($ch);
                curl_close($ch);
            }

            // Split header text into an array.
            $header_text = preg_split('/[\r\n]+/', $header);

            if ($_GET['mode'] == 'native') 
            {
                if (!$enable_native) 
                {
                    $contents = 'ERROR: invalid mode';
                    $status   = array(
                        'http_code' => 'ERROR'
                    );
                }

                // Propagate headers to response.
                foreach ($header_text as $header) 
                {
                    if (preg_match('/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header)) 
                    {
                        header($header);
                    }
                }

                print $contents;

            } 
            else 
            {

                // $data will be serialized into JSON data.
                $data = array();

                // Propagate all HTTP headers into the JSON data object.
                if ($_GET['full_headers']) 
                {
                    $data['headers'] = array();

                    foreach ($header_text as $header) 
                    {
                        preg_match('/^(.+?):\s+(.*)$/', $header, $matches);
                        if ($matches) 
                        {
                            $data['headers'][$matches[1]] = $matches[2];
                        }
                    }
                }

                // Propagate all cURL request / response info to the JSON data object.
                if ($_GET['full_status']) 
                {
                    $data['status'] = $status;
                } 
                else 
                {
                    $data['status']              = array();
                    $data['status']['http_code'] = $status['http_code'];
                }

                // Set the JSON data object contents, decoding it from JSON if possible.
                $decoded_json     = json_decode($contents);
                $data['contents'] = $decoded_json ? $decoded_json : $contents;

                // Generate appropriate content-type header.
                $is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
                header('Content-type: application/' . ($is_xhr ? 'json' : 'x-javascript'));

                // Get JSONP callback.
                $jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null;

                // Generate JSON/JSONP string`enter code here`
                $json = json_encode($data);

                print $jsonp_callback ? "$jsonp_callback($json)" : $json;

            }

0 个答案:

没有答案