hackerkts Posted January 1, 2010 Share Posted January 1, 2010 Hi, I'm kind of stuck. I'm trying to submit a form to another URL from my own web server, then retrieve the result by getting the page content. But it doesn't seems to work. The page I'm trying to get content is this http://epoly.tp.edu.sg/tpapp/isistt/TTServlet Use my admin for testing, 0800520I It's either you search by admin or the person name. Anyone has any idea? Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/ Share on other sites More sharing options...
cags Posted January 1, 2010 Share Posted January 1, 2010 Since the method of the form is post your best bet will probably be to use cURL, this allows you to send POST variables and retrieve the results. Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986778 Share on other sites More sharing options...
hackerkts Posted January 1, 2010 Author Share Posted January 1, 2010 Thanks for the reply! But I couldn't get it to work, this is what my friend had come out with <?php function PostRequest($url, $referer, $_data) { // convert variables array to string: $data = array(); while(list($n,$v) = each($_data)){ $data[] = "$n=$v"; } $data = implode('&', $data); // format --> test1=a&test2=b etc. // parse the given URL $url = parse_url($url); if ($url['scheme'] != 'http') { die('Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 $fp = fsockopen($host, 80); // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Referer: $referer\r\n"); //fputs($fp, "Expires: Mon, 26 Jul 2010 05:00:00 GMT\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as array: return array($header, $content); } $data = array( 'txtModule' => 'StudentSearch', 'txtSelStudentID' => '0800520I', 'cboSearch' => 'Student', 'txtAction' => 'TTServlet', 'txtAdmNo' => '0800520I'); // send a request to example.com (referer = jonasjohn.de) list($header, $content) = PostRequest("http://epoly.tp.edu.sg/tpapp/isistt/","http://localhost:80",$data); // print the result of the whole request: print $content; // print $header; --> prints the headers //print $header; ?> Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986786 Share on other sites More sharing options...
cags Posted January 1, 2010 Share Posted January 1, 2010 Define "but I couldn't get it to work". Did you read the documentation on cURL that I linked to, or even search for a simple curl post example on google? Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986796 Share on other sites More sharing options...
hackerkts Posted January 1, 2010 Author Share Posted January 1, 2010 I read that, but not all the articles. I'm trying out at the script my friend told me, no luck. And yes I did search, I wouldn't ask for help unless I really need to. Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986801 Share on other sites More sharing options...
cags Posted January 1, 2010 Share Posted January 1, 2010 http://davidwalsh.name/execute-http-post-php-curl Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986804 Share on other sites More sharing options...
hackerkts Posted January 1, 2010 Author Share Posted January 1, 2010 OMG, it works! Thanks cags(: Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986815 Share on other sites More sharing options...
cags Posted January 1, 2010 Share Posted January 1, 2010 Excellent. Don't forget to mark your topics as 'Solved' ('Mark Solved' button at the bottom left of threads you start). Quote Link to comment https://forums.phpfreaks.com/topic/186855-submit-form-externally-and-retrieve-the-result/#findComment-986821 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.