使用 cURL 和 PHP 执行网站的点击和注销


perform clicks and logout of website using cURL and php

我用cURL登录了一个网站。自然的问题是如何执行按钮点击而不是最终注销。例如。。JavaScript 使用 click(( 函数。php 使用什么?感谢您的线索。

我正在关注有关网络抓取的书。在其中,作者登录其出版商网站。这本书很旧,已经过时了。更重要的是,它没有说注销。这是发布者:https://www.packtpub.com/

你不能单独使用 PHP click按钮。PHP 不是那样工作的。PHP 可以下载网页的 HTML,但它不能像浏览器那样执行操作。

如果你想这样做,你将需要一个无头浏览器。无头浏览器通常被视为不可见的浏览器。您可以执行常规浏览器可以执行的大多数操作。有PhantomJS和CasperJS,为此。

还有一些使用PhantomJS的PHP库。例如PHP PhantomJS。就我个人而言,我从来没有用PHP做过这件事,但我确实经常使用PhantomJS和CasperJS。

除此之外,你可以用PHP做的是解析DOM中的链接或按钮,并复制单击链接/按钮时发出的HTTP请求。

例如,如果有一个指向 /contactus 的链接,您只需使用 cURL 创建对此页面的 GET 请求。响应将是源代码和/或标头。

我目前正在做一个项目,它使用CasperJS,PHP和Redis为大型社交网络创建一个相当复杂的抓取工具/自动化/分析工具。

作为旁注,一些网站严重依赖JavaScript,使用cURL可能还不够。你可以通过解析JavaScript文件和其他一些高级魔法来解决这个问题,但相信我,你不想走这条路。这就是为什么我有时使用CasperJS。它比较慢,但这就是我们目前所拥有的一切。

至于注销...删除您的饼干文件。做。

我最近发布了一个项目,该项目允许PHP访问浏览器。在这里得到它:https://github.com/merlinthemagic/MTS,引擎盖下是PhantomJS的一个实例,就像其他人建议的那样,这个项目只是包装了功能。

下载并设置后,您只需使用以下代码:

$myUrl          = "http://www.example.com";
$windowObj      = 'MTS'Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);
//select the username input field, in this case it has id=username
$windowObj->mouseEventOnElement("[id=username]", 'leftclick');
//type your username
$windowObj->sendKeyPresses("yourUsername");
//select the password input field, in this case it has id=passwd
$windowObj->mouseEventOnElement("[id=passwd]", 'leftclick');
//type your password
$windowObj->sendKeyPresses("yourPassword");
//click on the login button, in this case it has id=login
$windowObj->mouseEventOnElement("[id=login]", 'leftclick');
//click on all the buttons you need with this function
$windowObj->clickElement("[id=someButtonId]");
$windowObj->clickElement("[id=someOtherButtonId]");
//if you want the DOM or maybe screenshot and any point run:
$dom       = $windowObj->getDom();
$imageData = $windowObj->screenshot();