Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to another page within a PHP script

Tags:

post

php

request

How can I make a post request to a different php page within 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?

like image 384
QAH Avatar asked Aug 02 '09 00:08

QAH


People also ask

How can I send data from one page to another in PHP?

Simply call session_start() at the start of each page, and you can get and set data into the $_SESSION array.

How can I connect two HTML pages in PHP?

Link Submit button using Ancher Tags in PHP We can use Anchor tags to Link a Submit button to another page in PHP. We need to Write/Declare Submit button between Anchor tag's Starting and Closing tags. By using Anchor tag's href=”” attribute we can give a Path where we want to Link our Submit Button.

How can get ID from one page to another in PHP?

how to pass id from one page to another page using get and post method in php. <? php //$id= $_GET["tanent_id"]; //$id=$_GET["id"]; $con= mysql_connect("localhost","root","root"); if(!$

How post URL in PHP?

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' . $myvar1 .


1 Answers

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 = http_build_query($fields);  // 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).

2014 EDIT - well, it's been a while since I wrote that. These days it's worth checking Guzzle which again can work with or without the curl extension.

like image 193
Paul Dixon Avatar answered Sep 23 '22 19:09

Paul Dixon