PHPQuery WebBrowser Plugin - Using Cookies

I am trying to enter a site using the PHPQuery WebBrowser plugin. I can successfully log in, 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 to make other requests using session / login cookies?

+6
source share
1 answer

I looked at the source code to help you with this problem. My first impression was that the code was very poorly written. Debugging code is commented out, typos are everywhere, mile-long functions, etc. Perhaps you should consider switching to another solution in the long run, because if the author changes something in this code, you may end up in your own code with the update.

At the same time, the WebBrowser plugin gives you access to the browser object itself, which contains the getLastResponse () function. This returns a Zend_Http_Response object, which could theoretically be used to receive cookies.

The problem is that you have no way to set these cookies. You will need to fix the web browser plugin somewhere near line 102 to include your own HTTP request object (parameter 2 for phpQuery::ajax() ) with your cookie, here:

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

Alternatively, you can also correct the phpQuery.php line 691 line to include a global cookie jar, which you could define as singleton or so. (Right where $client->setCookieJar(); indicated).

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

+3
source

All Articles