PHP, How to Redirect/Forward HTTP Request with header and body?

前端 未结 3 1381
故里飘歌
故里飘歌 2021-01-30 09:14

I have a PHP page, main.php which is on server 1.

I have a PHP page main.php (same page, different code) on server 2.

main.php is a WebService.

I would l

3条回答
  •  后悔当初
    2021-01-30 10:10

    I have used the code by Rehmat and Calmarius and made a few changes so now it handles multiple fields with same name like

    
    
    
    

    and to upload files too, including multiple files that use the same field name.

    here is goes:

    sanitizePostFields($_POST);
                $files  = $this->sanitizeFiles($_FILES);
                if ($files) {
                    $post = array_merge($post, $files);
                }
                
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                /*
                // this is enough if not uploading files
                curl_setopt(
                    $ch,
                    CURLOPT_POSTFIELDS,
                    http_build_query($_POST)
                );
                */
            }
            
            curl_setopt($ch, CURLOPT_URL, $this->site . $request);
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
    
            $headers = getallheaders();
    
            /*
                Translate some headers to make the remote party think we
                actually browsing that site.
            */
            $extraHeaders = array();
            if (isset($headers['Referer'])) {
                $extraHeaders[] = 'Referer: '. str_replace(
                    $this->proxyDomain,
                    $this->remoteDomain,
                    $headers['Referer']
                );
            }
            if (isset($headers['Origin'])) {
                $extraHeaders[] = 'Origin: '. str_replace(
                    $this->proxyDomain,
                    $this->remoteDomain,
                    $headers['Origin']
                );
            }
    
            /*
                Forward cookie as it came.
            */
            curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders);
            if (isset($headers['Cookie'])) {
                curl_setopt($ch, CURLOPT_COOKIE, $headers['Cookie']);
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
            if ($this->logHeaders) {
                $f = fopen("headers.txt", "a");
                curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
                curl_setopt($ch, CURLOPT_STDERR, $f);
            }
    
            //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            $response = curl_exec($ch);
    
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $headers = substr($response, 0, $header_size);
            $body = substr($response, $header_size);
    
            $headerArray = explode(PHP_EOL, $headers);
    
            /* Process response headers. */
            foreach($headerArray as $header) {
                $colonPos = strpos($header, ':');
                if ($colonPos !== FALSE) {
                    $headerName = substr($header, 0, $colonPos);
    
                    /*
                        Ignore content headers, let the webserver decide how to
                        deal with the content.
                    */
                    if (trim($headerName) == 'Content-Encoding') continue;
                    if (trim($headerName) == 'Content-Length') continue;
                    if (trim($headerName) == 'Transfer-Encoding') continue;
                    
                    //if (trim($headerName) == 'Location') continue;
                    
                    /* -- */
                    /* Change cookie domain for the proxy */
                    if (trim($headerName) == 'Set-Cookie') {
                        $header = str_replace(
                            'domain='.$this->remoteDomain,
                            'domain='.$this->proxyDomain,
                            $header
                        );
                    }
                    /* -- */
                    
                    if (trim($headerName) == 'Location') {
                        $header = str_replace(
                            $this->remoteDomain,
                            $this->proxyDomain,
                            $header
                        );
                    }
                }
                
                header($header, FALSE);
            }
    
            echo $body;
    
            if ($this->logHeaders) {
                fclose($f);
            }
            
            curl_close($ch);
        }
    
        private function sanitizePostFields($post, $fieldName = '') {
            if (empty($post)) { return false; }
            if (!is_array($post)) { return false; }
            
            $result = [];
            
            foreach ($post as $k => $v) {
                if (is_string($v)) {
                    $result[($fieldName ? "{$fieldName}[{$k}]" : $k)] = $v;
                }
                elseif (is_array($v)) {
                    $result = array_merge(
                        $result,
                        $this->sanitizePostFields($v, $k)
                    );
                }
            }
            
            return $result;
        }
    
        private function sanitizeFiles($files) {
            if (empty($files)) { return false; }
            if (!is_array($files)) { return false; }
            
            $result = [];
            
            foreach ($files as $k => $v) {
                if (empty($v['name'])) { continue; }
                
                if (is_array($v['name'])) {
                    // more than one file using the same name field[]
                    $nFields = count($v['name']);
                    for ($i = 0; $i < $nFields; $i++) {
                        if (empty($v['tmp_name'][$i])) { continue; }
                        $curl_file_upload = new CURLFile(
                            $v['tmp_name'][$i],
                            $v['type'][$i],
                            $v['name'][$i]
                        );
                        $result["{$k}[{$i}]"] = $curl_file_upload;
                    }
                }
                else {
                    $curl_file_upload = new CURLFile(
                        $v['tmp_name'],
                        $v['type'],
                        $v['name']
                    );
                    $result[$k] = $curl_file_upload;
                }
            }
            
            return $result;
        }
        
    }
    
    $proxy = new proxy();
    

提交回复
热议问题