Pufas Posted November 13, 2010 Share Posted November 13, 2010 Hi there, OK, there is what I'm trying to accomplish - I want to send POST instantly when the page is loaded without "Submit" button or anything like that. Here how it looks using JavaScript: <form name="postdata" action="http://www.mywebsite.com/post.php" method="post"> <input name="data" type="text" value="1"/> </form> <script type="text/javascript" language="JavaScript"> document.postdata.submit(); </script> Works fine with JavaScript, however i can't use it. Here is reference for CURL: http://php.net/manual/en/book.curl.php I found this curl_post function at php.net to simplify things up. <?php /** * Send a POST requst using cURL * @param string $url to request * @param array $post values to send * @param array $options for cURL * @return string */ function curl_post($url, array $post = NULL, array $options = array()) { $defaults = array( CURLOPT_POST => 1, CURLOPT_HEADER => 0, CURLOPT_URL => $url, CURLOPT_FRESH_CONNECT => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FORBID_REUSE => 1, CURLOPT_TIMEOUT => 4, CURLOPT_POSTFIELDS => http_build_query($post) ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); if( ! $result = curl_exec($ch)) { trigger_error(curl_error($ch)); } curl_close($ch); return $result; } $test = curl_post("http://www.mywebsite.com/post.php", array("data" => "1")); print($test); ?> Looks like it uses GET instead POST (used sniffer to verify this). I'm trying to send POST data to other host, if that matters. Any help would be appreciated. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 13, 2010 Share Posted November 13, 2010 Looks like it uses GET instead POST (used sniffer to verify this). why sniff to determine whether it posts or gets? check from post.php: echo $_SERVER['REQUEST_METHOD']; Quote Link to comment Share on other sites More sharing options...
Pufas Posted November 13, 2010 Author Share Posted November 13, 2010 I'm sorry, but I'm real noob at PHP. I've tried what you said, BlueSkyIS echo $_SERVER['REQUEST_METHOD']; outputs 'GET' :/ Any ideas guys? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 using the same code, I get POST from my server. Quote Link to comment Share on other sites More sharing options...
savagenoob Posted November 14, 2010 Share Posted November 14, 2010 Maybe set your CURL_SETOPTS manually and see what happens in the function? like curl_setopt($ch, CURLOPT_POST, 1); I'm just throwing a fart in the wind... Quote Link to comment Share on other sites More sharing options...
Pufas Posted November 14, 2010 Author Share Posted November 14, 2010 I've found another function that does work: <?php $data = array(); $data['data'] = '1'; $post_str = ''; foreach($data as $key=>$val) { $post_str .= $key.'='.urlencode($val).'&'; } $post_str = substr($post_str, 0, -1); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.mywebsite.com/post.php' ); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); echo $result; ?> But once I got i running I found out it's not quiet what i wanted if the first place. Using CURL, POST is send by the server (where this, where php script that's above is being hosted) to 'http://www.mywebsite.com/post.php'. But I want POST to be send by the user who browses my other web page (let's call it www.myfirstwebpage.com). So, using curl i get this: www.myfirstwebpage.com ---(POST)---> www.mywebsite.com But that's what i want (i get it using JavaScript): User ---(POST)---> www.mywebsite.com So put it simple i want to User to POST "data=1" to www.mywebsite.com automatically once the page is loaded. Is it possible to do this via php? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 Is it possible to do this via php? not easily, if at all. you want php to pretend it is the user, down to their browser and their IP address. and that's probably the biggest problem: spoofing the ip address of the user. i don't know if you could get that to work. Quote Link to comment Share on other sites More sharing options...
Pufas Posted November 14, 2010 Author Share Posted November 14, 2010 Takes only one line using JavaScript. :/ <script type="text/javascript" language="JavaScript"> document.postdata.submit(); </script> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 The humongous difference is that Javascript is client-side while PHP is server-side. From my browser, I can turn off processing of Javascript with a click. I can't turn off processing of PHP from my browser. Quote Link to comment Share on other sites More sharing options...
Pufas Posted November 15, 2010 Author Share Posted November 15, 2010 Thanks for all the info, marking this as solved. Quote Link to comment 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.