cURL Post Foward

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

cURL Post Foward

Post by tecktalkcm0391 »

Hello,

I'm having problems taking everything that is posted to a page and sending it via cURL... I just can't figure it out... how do I take everything posted to pageA and send it to pageB on a different server.... each time the POST has different information..


Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: cURL Post Foward

Post by Christopher »

What part is not working? You need to take the $_POST vars and build a cURL request from them. See the manual or be more specific on where the problem is occurring.
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: cURL Post Foward

Post by JellyFish »

I didn't know you could make HTTP POST requests with cURL? I thought your only option was fsockopen.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: cURL Post Foward

Post by Christopher »

cURL has options: CURLOPT_HTTPGET, CURLOPT_POST, CURLOPT_PUT, plus CURLOPT_CUSTOMREQUEST.
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: cURL Post Foward

Post by JellyFish »

Awesome! Thanks arborint for that epiphany!

I managed to wip up a cURL that sends POST requests to a page. If you want all the $_POST fields to be sent to another page that's on another server try something like this:

Code: Select all

 
<?php
$data = $_POST;
 
$ch = curl_init("http://url.com/path/file");
 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CUROPT_POSTFIELDS, $data));
 
curl_exec($ch)
curl_close($ch);
?>
 
Something like that. You can also only send individual fields by redefining the $data definition:

Code: Select all

 
$data = array("name" => $_POST['name'], "whatever" => $_POST['whatever']);
 
Just as long as $data is an array it'll work.

Hope that helps.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: cURL Post Foward

Post by Christopher »

To quote Kieran (twice) -- I heart cURL and cURL FTW!
(#10850)
Post Reply