tags:

views:

512

answers:

5

Hello! How can I make a post request to another php page withing a php script? I have one front end computer as the html page server, but when the user clicks a button, I want a backend server to do the processing and then send the information back to the front end server to show the user. I was saying that I can have a php page on the back end computer and it will send the information back to the front end. So once again, how can I do a POST request to another php page, from a php page?

Thanks

+1  A: 

For PHP processing, look into cURL. It will allow you to call pages on your back end and retrieve data from it. Basically you would do something like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$fetch_url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch,CURLOPT_USERAGENT, $user_agent;
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);

You can also look into the PHP HTTP Extension.

Chacha102
I know about ajax. My page is using ajax, but I want in other words, inner network communication between my front end server and my back end. I don't want to user to be able to directly communicate with the back end server. So how would I let the front end talk with the back end using php?
QAH
I downvoted as the OP wasn't asking how to achieve this on the client side, but on the server side.
Paul Dixon
He said press a button, process, and then show the data. In almost every other question, people immediately look to AJAX.
Chacha102
Ok, jQuery is completely removed, and instead cURL is in place. Sorry for the misunderstanding
Chacha102
+3  A: 

Possibly the easiest way to make PHP perform a POST request is to use cURL, either as an extension or simply shelling out to another process. Here's a post sample:

//where are we posting to?
$url = 'http://foo.com/script.php';

//what post fields?
$fields = array(
   'field1'=>$field1,
   'field2'=>$field2
);

//build the urlencoded data
$postvars='';
$sep='';
foreach($fields as $key=>$value) 
{ 
   $postvars.= $sep.urlencode($key).'='.urlencode($value); 
   $sep='&'; 
}


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Also check out Zend_Http set of classes in the Zend framework, which provides a pretty capable HTTP client written directly in PHP (no extensions required).

Paul Dixon
Keys need to be urlencoded as well.
cletus
you are correct, I'm in the habit of choosing URL-safe keys though, but will modify the sample...
Paul Dixon
If you don't own your server, theres a chance you may run into the issue that PHP, and thus cURL, are not allowed to leave the local network.
Shadow
+1  A: 

Assuming your php install has the CURL extension, it is probably the easiest way (and most complete, if you wish).

Sample snippet:

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
                      'lname'=>urlencode($last_name),
                      'fname'=>urlencode($first_name),
                      'email'=>urlencode($email)
               );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Credits go to http://php.dzone.com. Also, don't forget to visit the appropriate page(s) in the PHP Manual

jcinacio
+1  A: 
  1. Like the rest of the users say it is easiest to do this with CURL.

  2. If curl isn't available for you then maybe http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

  3. If that isn't possible you could write sockets yourself http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html

Alfred
A: 

CURL method is very popular so yes it is good to use it. You could also explain more those codes with some extra comments because starters could understand them.

Pico RG