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

前端 未结 3 1461
故里飘歌
故里飘歌 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:06

    this is the solution i have found (there might be better)

     public static function getResponse ($url,$headers,$body)
        {
            $params = '?' . http_build_query($headers);
    
            $redirect_url = $url . $params;
    
            $ch = curl_init($redirect_url);
    
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);
    
            if (!isset($response))
                return null;
            return $response;
        }
    

提交回复
热议问题