PHPQuery web浏览器插件 - 使用cookie [英] PHPQuery WebBrowser plugin - using cookies

查看:797
本文介绍了PHPQuery web浏览器插件 - 使用cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录到使用PHPQuery的web浏览器插件网站。我能够成功登录,但我不知道如何从previous调用next重用饼干。

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?

推荐答案

我已经采取了看看源$ C ​​$ C以帮助您解决这个问题。我的第一个IM pression是,code写很差。调试code注释掉,错别字满天飞,一英里长的功能等你真的可能要考虑切换到从长远来看,不同的解决方案,因为如果笔者改变的东西在这个code,你可能最终拥有自己的code破与升级。

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.

话虽这么说,web浏览器插件,您可以访问浏览器对象本身,它包含了一个名为使用getLastResponse()函数。这会返​​回一个 Zend_Http_Response 对象,您可以理论上使用得到的cookie。

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.

问题是,你没有设置这些Cookie的任何方式。你将不得不打补丁的Web浏览器周围102线某处插件,包括你自己的HTTP请求对象(参数2 phpQuery ::阿贾克斯())与您的Cookie设置,围绕这里:

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',
));

另外,您还可以修补 phpQuery.php 线691包括一个全球性的饼干罐,你可以定义为一个单左右。 (右那里说 $客户 - > setCookieJar(); )。

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();).

此外,这code被写入很差,可能就好得多使用生卷曲呼吁即使它缺乏一个位的功能。

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.

这篇关于PHPQuery web浏览器插件 - 使用cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆