PHPQuery WebBrowser plugin - using cookies

爷,独闯天下 提交于 2020-01-02 03:49:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14481677/phpquery-webbrowser-plugin-using-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!