severndigital Posted July 9, 2009 Share Posted July 9, 2009 I am working on script that logs me into a site and preforms a search on that site using the HttpRequest / HttpResponse classes. I have them all installed, configured and working. I just can't seem to figure out the usage. I've been on the php.net pages for a few hours now and just can't make heads or tails of what's going on. here is what I have so for $url = 'my.site.com'; $username = 'username'; $password = 'password'; $login = new HttpRequest($url . "/Login"); // I figure I need to get the fields or set the fields, but I can't figure out which method actually does that. then once it does, how do get the reponse back from the site? i thought I would be using the httpReponse to do that, but I'm not so sure. Any help or a point to a good tutorial would be great. Thanks, -P Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 9, 2009 Share Posted July 9, 2009 try this <?php $url = 'my.site.com'; $username = 'username'; $password = 'password'; $login = new HttpRequest($url. "/Login", HttpRequest::METH_POST); //The array Keys are the post keys $post_data = array("Username"=>$username,"Password"=>$password); $login->addPostFields($post_data); try { echo $r->send()->getBody(); } catch (HttpException $ex) { echo $ex; } ?> EDIT:updated to fit what you have Quote Link to comment Share on other sites More sharing options...
severndigital Posted July 9, 2009 Author Share Posted July 9, 2009 echo $r->send()->getBody(); can you explain this line please?? the rest makes sense.. thanks. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 $r->send() Sends the HTTP request. and Returns the received response as HttpMessage object. getBody() is an object that is returned that has the contents you could also do this $r->send(); echo $r->getResponseBody(); Quote Link to comment Share on other sites More sharing options...
severndigital Posted July 10, 2009 Author Share Posted July 10, 2009 thank you for that. I got it working sort of, I believe that the information is going in, but the wrong submit button is being clicked. is there a way to control which form is being populated and submitted? here is what I have so far $DSUrl = 'my.site.com'; $user = 'username'; $password = 'password'; $login = new HttpRequest($DSUrl . '/Login'); $post_data = array("username"=>$user,"password"=>$pass); $login->addPostFields($post_data); try { $login->send(); echo $login->getResponseBody(); }catch(HttpException $ex){ echo $ex; } When i run the code the log in page appears on my screen so it is going and getting the page. There is a search form at the top and the login form is in the middle. Thanks for any help again, -P Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 it depends on the form.. read the html form, yoy use to login get the fields and action.. and add them to the array Quote Link to comment Share on other sites More sharing options...
severndigital Posted July 10, 2009 Author Share Posted July 10, 2009 First off .. i really appreciate all you help. it just doesn't seem like it is submitting the form. correct me if i am wrong, but i when I echo $login->getResponseBody(). I should be seeing the page I would see if i was logged into the site... correct?? Here is my code thus far. $DSUrl = 'http://www.mysite.com'; $user = "username"; $pass = "password"; $login = new HttpRequest($DSUrl . '/Login',HttpRequest::METH_POST); $post_data = array("username"=>$user,"password"=>$pass); $login->addPostFields($post_data); $post_file = $DSUrl . "/ApplyLogin"; //form action $login->addPostFile("Login",$post_file); try { $login->send(); echo $login->getResponseBody(); }catch(HttpException $ex){ echo $ex; } I believe that is correct, but all i get is the login page over and over again. ***EDIT*** I added the HttpRequest::METH_POST and got the following exception exception 'HttpInvalidParamException' with message 'Empty or too short HTTP message Also one thing i noticed the form and the submit button are both named "Login" .. could that be causing the problem?? Quote Link to comment Share on other sites More sharing options...
severndigital Posted July 10, 2009 Author Share Posted July 10, 2009 ok so I learned that addPostFile is for uploading files .. how do I tell the request which form on the page to submit? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 Thats the URL.. thats where your submitting too! Quote Link to comment Share on other sites More sharing options...
severndigital Posted July 10, 2009 Author Share Posted July 10, 2009 Ahhh .. ok I was starting a new request class with the login page. what I needed to do was start the class with the url that is running the login script. AHHHHH ... OK .. well it's giving me an error, but It is posting the information now. -Thanks, sorry for confusion. -P Quote Link to comment Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 Goto the login form and view source, the action is the URL (of course your probably need to add the domain name) the fields will have names and values make sure the array has these, and that should be it 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.