Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPQuery WebBrowser plugin - using cookies

I'm trying to login into a website using PHPQuery's WebBrowser plugin. I'm able to successfully login but I'm not sure how to reuse cookies from a previous call to the next.

$client = phpQuery::browserGet('https://website.com/login', 'success1');

function success1($browser) {
  $handle = $browser
    ->WebBrowser('success2');
  $handle 
    ->find('input[name=name]')
      ->val('username');
  $handle 
    ->find('input[name=pass]')
      ->val('password')
      ->parents('form')
        ->submit();
}

function success2($browser) {
    print $browser; // prints page showing I'm logged in

    // make authenticated requests here
}

How do I make other requests with session/login cookies?

like image 966
Paramount Avatar asked Jan 23 '13 14:01

Paramount


1 Answers

I've taken a look at the source code to aid you with this problem. My first impression was that the code was very poorly written. Debugging code commented out, typos all over the place, mile-long functions, etc. You really might want to consider switching to a different solution in the long run because if the author changes something in this code, you might end up having your own code broken with an upgrade.

That being said, the WebBrowser plugin gives you access to the browser object itself, which contains a function called getLastResponse(). This returns a Zend_Http_Response object, which you can theoretically use to get the cookies.

The problem is that you don't have any way of setting those cookies. You would have to patch the web browser plugin somewhere around line 102 to include your own HTTP request object (parameter 2 for phpQuery::ajax()) with your cookies set, around here:

$xhr = phpQuery::ajax(array(
    'type' => 'GET',
    'url' => $url,
    'dataType' => 'html',
));

Alternatively you could also patch phpQuery.php line 691 to include a global cookie jar you could define as a singleton or so. (Right where it says $client->setCookieJar();).

Again, this code is very poorly written, you are probably MUCH better off using raw curl calls, even if it lacks a bit of functionality.

like image 88
Janos Pasztor Avatar answered Oct 19 '22 14:10

Janos Pasztor