note 93062 deleted from function.header by danbrown

From: Date: Fri, 21 Aug 2009 13:29:45 +0000
Subject: note 93062 deleted from function.header by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159734@lists.php.net to get a copy of this message
Note Submitter: johan162 at gmail dot com ---- In response to "rbawaskar at gmail dot com" and others regarding the use of header() to simulate a POST request using the header() function. 1. Why you cannot simulate a POST request with header() ======================================================= It is NOT possible to properly simulate a POST request with the header() function. There seems to be a common misunderstanding about what header() does. header() function sends HTTP header(s) TO the client (i.e web-browser) from the server. POST request is an HTTP header sent FROM the client to the server. +----------+ header() +----------+ | Server | ---------------> | Browser | +----------+ +----------+ +----------+ POST request +----------+ | Browser | ------------------> | Server | +----------+ +----------+ Hence it is is never possible to "simulate" a POST call with the use of the header() function. There are basically three (correct) ways to simulate a POST request as shown below. However there is crucial difference compared with the original form post data from the browser to the server. After issuing a POST request the browser automatically redirects to show the reply from the POST request as a "new" page (by default using the same target window as the request was made from, by manually setting a different target it is also possible to force the reply from a PST request to be shown in a different window). This is not possible to do when sending a "fake" post request to a page since we are not the browser. The best we can accomplish is to show the reply inline in the calling page which are then shown in the browser 2. Alternatives to simulate a POST request ========================================== 2.1 Create a stream request ============================= function sendPostData_v1($url, $data) { $opts = array('http' => array('method' => 'post', 'content' => $data)); $stream = stream_context_create($opts); $fp = fopen($url, 'rb', false, $stream); if (!$fp) { // Some error handling } // Find out what the page returns as its body $reply = stream_get_contents($fp); if ($reply === false) { // Some error handling } return $reply; } 2.2 Open a socket directly and write to it =========================================== function sendPostData_v2($url,$data,$port=80) { $errno=-1;$errstr=''; $fs = fsockopen($url,$port,$errno,$errstr); if( $fs === false ) { // Some error handling } $header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($data) . "\r\n\r\n"; fputs($fs, $header . $data ); // Find out what the page returns as its body $reply = ''; while( !feof($fs) ) { $reply .= fgets($fp,8192); } return $reply; } 2.3 Use cURL to handle all the necessary details ================================================= Note 1: This requires that curl libraries are installed and that curl is enabled in the PHP installations. Note 2: Depending on your application there might be many more options that you want to tweak. The one I have used below is just the bare necessities. function sendPostData_v3($url,$data) { // Initialize and get the curl handle $ch = curl_init(); // Include possible headers in the reply from the server as well curl_setopt( $ch, CURLOPT_HEADER, true ); // Return the reply as a string from curl_exec() instead of directly to stdout curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // The URI we want to send the post data to curl_setopt($ch, CURLOPT_URL,$url); // Use the POST method curl_setopt($ch, CURLOPT_POST, true); // All the data to be sent curl_setopt($ch, CURLOPT_POSTFIELDS,$data); $reply = curl_exec ($ch); curl_close($ch); return $reply; }

« previous php.notes (#159734) next »