severndigital Posted July 14, 2009 Share Posted July 14, 2009 I've been working on a script to automatically log into one of our internal systems and perform search functions. I need to log into the system to do this. I used the HttpRequest() class to get in. But I've been having some problems. Apparently the login form has a hidden field in it called login_token that needs to be sent along with the post data. the login page and the action for the form are two different pages. how do i read the value of the login_token field and pass it along with the post data to the login action? here is what I have so far. $DSUrl = 'http://my.site.com'; $user = "username"; $pass = "password"; $login = new HttpRequest($DSUrl . '/ApplyLogin',HttpRequest::METH_POST); $post_data = array("username"=>$user,"password"=>$pass,"domain"=>'DocuShare'); //$post_data = array("username"=>$user,"password"=>$pass); $login->addPostFields($post_data); //$login->addHeaders($headers); try { echo $login->send()->getBody(); }catch(HttpException $ex){ echo $ex; } Thanks in advance. -P Link to comment https://forums.phpfreaks.com/topic/165910-read-a-form-and-then-submit-it/ Share on other sites More sharing options...
ILMV Posted July 14, 2009 Share Posted July 14, 2009 I do not know the exact method, but you will need to hit the page and search the source for the hidden field, there must be a method of grabbing the contents between two set values. Something like this might help: $content=''; // YOUR HTML SOURCE HERE $word1='<input type="hidden" name="token" value="'; $word2='" />'; $between=substr($contents, strpos($contents, $word1), strpos($contents, $word2) - strpos($contents, $word1)); I haven't tested this code, you will have to set your HTML to $content, and modify $word1 and $word2 to match the code around your hidden field. It might be benificial to remove line breaks in your HTML before testing it. I hope this helps. ILMV Link to comment https://forums.phpfreaks.com/topic/165910-read-a-form-and-then-submit-it/#findComment-875124 Share on other sites More sharing options...
phporcaffeine Posted July 14, 2009 Share Posted July 14, 2009 Here is a function that you can call to make POST requests against resource locators (should be all you need). function makePost($url, $referer, $_data) { $data = array(); while(list($n,$v) = each($_data)){ $data[] = "$n=$v"; } $data = implode('&', $data); $url = parse_url($url); if ($url['scheme'] != 'http') { die('Only HTTP request are supported !'); } $host = $url['host']; $path = $url['path']; //CREATE A SOCKET $fp = fsockopen($host, 80); //HEADERS fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Referer: $referer\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)) { $result .= fgets($fp, 128); } fclose($fp); $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; return array($header, $content); } $data = array( 'field1' => 'value1', 'field2' => 'value2', 'field3' => 'value3' ); list($header, $content) = makePost("http://www.example.com/", "http://www.referersite.com/", $data); print $content; Link to comment https://forums.phpfreaks.com/topic/165910-read-a-form-and-then-submit-it/#findComment-875125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.