mkosmosports Posted September 28, 2007 Share Posted September 28, 2007 Hey, page.php contains some $_POST array data that I want automatically passed on to page1.php, without the use of a form. So.......... On page.php I formed a post request in a variable like so: $host = "localhost"; $uri = "page1.php"; $data = $_POST['data1']; $contentlength = strlen($data); $reqheader = "POST $uri HTTP/1.1\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $contentlength\r\n\r\n". "$data\r\n"; Now what do I do with this? I tried opening page1.php and writing the post request to it: $handle = fopen($url, "r"); fwrite($handle, $reqheader); But now what? At this point I want to go to page1.php and see the posted data there, just as I would with a form with an action of "page1.php". Any help is much appreciated! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/ Share on other sites More sharing options...
chronister Posted September 28, 2007 Share Posted September 28, 2007 call me kooky, but why not use a $_SESSION for this?? or pass it in the URL as a $_GET Nate Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-356999 Share on other sites More sharing options...
mkosmosports Posted September 28, 2007 Author Share Posted September 28, 2007 Hey chronister, I dont want to use sessions as it will complicate things for me, considering my script, plus then I become dependent on cookies, which I dont want to be. I dont want to pass it in the URL as a GET because I dont want this data to be seen by users. Ive got the solution though, to use cURL, which is luckily available on my hosts server, but I think its available on most. <? $myURL="http://www.remoteSite.com/login.php"; $chttp = curl_init(); curl_setopt($chttp, CURLOPT_URL,"$myURL"); //URL to post the data to curl_setopt($chttp, CURLOPT_POST, 1); curl_setopt($chttp, CURLOPT_POSTFIELDS, "key1=value1&key2=value2"); //POST names (key1, key2) and their respective values curl_exec ($chttp); curl_close ($chttp); ?> Ive also found a way to do this without cURL, as seen in this link: http://www.zend.com/zend/spotlight/mimocsumissions.php Although I cant seem to get this one working.. mkosmosports Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357107 Share on other sites More sharing options...
blueman378 Posted September 28, 2007 Share Posted September 28, 2007 just one question would this be causing problems? $host = "localhost"; $uri = "page1.php"; $data = $_POST['data1']; $contentlength = strlen($data); $reqheader = "POST $uri HTTP/1.1\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $contentlength\r\n\r\n". "$data\r\n"; Now what do I do with this? I tried opening page1.php and writing the post request to it: $handle = fopen($url, "r"); fwrite($handle, $reqheader); just a thought Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357130 Share on other sites More sharing options...
mkosmosports Posted September 28, 2007 Author Share Posted September 28, 2007 Yes that would certainly be causing problems blueman. Thanks. Ive got some more changes coming, and I believe I can now successfully send the post request (Thanks to that Zend article!), but Im still failing to redirect to that page at the same time just as a form post script would. (I know how to do it with cURL but I want to know and understand this way as well) So, this is test.php... //Sending the deal URL through a POST to test1.php function post_it($datastream, $url) { //Replace 'http://' with the empty string //Get the Host address substring //Get the URI of the desired Resource $_url = preg_replace("@^http://@i", "", $url); $host = substr($_url, 0, strpos($_url, "/")); $uri = strstr($_url, "/"); //Initialize the $reqbody variable //Traverse the $DataStream array //If $reqbody is not empty, append a ampersand sign '&' //Append the variable/value pair to the end of $reqbody $reqbody = ""; foreach($datastream as $key=>$val) { if (!empty($reqbody)) $reqbody.= "&"; $reqbody.= $key."=".urlencode($val); } //Construct the POST request to send to the desired server. $contentlength = strlen($reqbody); $reqheader = "POST $uri HTTP/1.0\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $contentlength\r\n\r\n". "$reqbody\r\n"; //Connect to the server using fsockopen() //If there was a connect failure, return in error //Send the POST request to the server $socket = fsockopen($host, 80, $errno, $errstr); if (!$socket) { $result["errno"] = $errno; $result["errstr"] = $errstr; return $result; } fputs($socket, $reqheader); fclose($socket); return $result; } //Now to send the below array, using the post_it function above $_data["d1"] = "I want to send this data over"; $result = post_it($_data, "http://localhost/test1.php"); This seems to workNow how do I get it to redirect to "http://localhost/test1.php" like a normal form post request would do?? Thanks for any suggestions! Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357133 Share on other sites More sharing options...
blueman378 Posted September 28, 2007 Share Posted September 28, 2007 sorry if u have already fixed it but i figure if u havnt ill point it out u have the same thing here: //Sending the deal URL through a POST to test1.php function post_it($datastream, $url) { //Replace 'http://' with the empty string //Get the Host address substring //Get the URI of the desired Resource $_url = preg_replace("@^http://@i", "", $url); $host = substr($_url, 0, strpos($_url, "/")); $uri = strstr($_url, "/"); //Initialize the $reqbody variable //Traverse the $DataStream array //If $reqbody is not empty, append a ampersand sign '&' //Append the variable/value pair to the end of $reqbody $reqbody = ""; foreach($datastream as $key=>$val) { if (!empty($reqbody)) $reqbody.= "&"; $reqbody.= $key."=".urlencode($val); } //Construct the POST request to send to the desired server. $contentlength = strlen($reqbody); $reqheader = "POST $uri HTTP/1.0\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $contentlength\r\n\r\n". "$reqbody\r\n"; //Connect to the server using fsockopen() //If there was a connect failure, return in error //Send the POST request to the server $socket = fsockopen($host, 80, $errno, $errstr); if (!$socket) { $result["errno"] = $errno; $result["errstr"] = $errstr; return $result; } fputs($socket, $reqheader); fclose($socket); return $result; } //Now to send the below array, using the post_it function above $_data["d1"] = "I want to send this data over"; $result = post_it($_data, "http://localhost/test1.php"); or is it intentional? if so please tell me why Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357144 Share on other sites More sharing options...
mkosmosports Posted September 28, 2007 Author Share Posted September 28, 2007 Hey blueman, In this case it is intentional yes. The uri variable holds the part of the url after the domain, so the path/directory to the page name (including that page name as well). This is actually pretty confusing on my part as the uri IS the full url, but in this case the above is what this variable does. mkosmosports Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357410 Share on other sites More sharing options...
blueman378 Posted September 29, 2007 Share Posted September 29, 2007 ah right i get that, thanks for explaining it Quote Link to comment https://forums.phpfreaks.com/topic/70986-mimicking-post-request-to-one-of-my-own-pages/#findComment-357711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.