Jump to content

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/165396-solved-httprequest-response-trouble/
Share on other sites

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

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

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??

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.