是否有一个PHP equivalent of Perl的WWW::机械化?
-
11-07-2019 - |
题
我在寻找一个图书馆具有功能上类似于Perl's WWW::机械化, ,但是对于PHP.基本上,它应该允许我提交HTTP GET and POST请求用一个简单的语法,然后分析结果页上和返回的一个简单的格式一切形式和自己的领域,以及所有链接的网页。
我知道关于卷曲,但它是一个有点太瘦骨嶙峋的人,并在语法是很丑陋(吨 curl_foo($curl_handle, ...)
报表
澄清:
我想要的东西更高水平比答案为止。例如,在Perl,你可以做一些事情,如:
# navigate to the main page
$mech->get( 'http://www.somesite.com/' );
# follow a link that contains the text 'download this'
$mech->follow_link( text_regex => qr/download this/i );
# submit a POST form, to log into the site
$mech->submit_form(
with_fields => {
username => 'mungo',
password => 'lost-and-alone',
}
);
# save the results as a file
$mech->save_content('somefile.zip');
做同样的事情使用HTTP_Client或wget或卷曲,将大量的工作,我必须手动分析网页找到的链接,现形式网址,提取的所有的隐藏的领域,等等。原因我要求PHP的解决方案是,我没有经验Perl,我或许可以建立什么我需要有很多工作,但它会更快,如果我可以做到上述PHP.
解决方案
SimpleTest的的 ScriptableBrowser 可以independendly从测试框架中使用。我已经使用了许多自动化的作业。
其他提示
我觉得有必要回答这个问题,即使其旧的文章......我一直在使用PHP卷曲了很多,这是不是好事近媲美任何地方,像WWW:机械化,这我切换到(我想我会用Ruby语言实现去)..卷曲是过时的,因为它需要太多“繁重的工作”自动化的话,就是SimpleTest的编写脚本的浏览器看起来前途无量我,但在测试它,它不会工作在大多数Web窗体我试试......说实话,我觉得PHP缺乏这一类刮,网络自动化的所以最好看一下不同的语言,只是想发布此,因为我已经在这个题目花了无数时间也许这将节省别人一些时间在未来。
现在的2016和有水貂。它甚至支持从无头纯PHP“浏览器”(没有JavaScript)不同的发动机,用硒(这需要像Firefox或Chrome浏览器),在NPM无头“browser.js”,它不支持JavaScript。
尝试寻找在PEAR库。如果一切都失败,创建用于卷曲的对象的包装。
您可以这么简单的东西是这样的:
class curl {
private $resource;
public function __construct($url) {
$this->resource = curl_init($url);
}
public function __call($function, array $params) {
array_unshift($params, $this->resource);
return call_user_func_array("curl_$function", $params);
}
}
卷曲是去简单的请求的方式。它运行跨平台,具有PHP扩展并且被广泛采用并进行测试。
我创建了一个很好的类,它可以GET和POST数据的阵列(包括档案!)到URL通过只调用CurlHandler ::获取($网址,$数据)|| CurlHandler ::邮报($网址,$数据)。还有一个可选的HTTP用户认证选项太:)
/**
* CURLHandler handles simple HTTP GETs and POSTs via Curl
*
* @package Pork
* @author SchizoDuckie
* @copyright SchizoDuckie 2008
* @version 1.0
* @access public
*/
class CURLHandler
{
/**
* CURLHandler::Get()
*
* Executes a standard GET request via Curl.
* Static function, so that you can use: CurlHandler::Get('http://www.google.com');
*
* @param string $url url to get
* @return string HTML output
*/
public static function Get($url)
{
return self::doRequest('GET', $url);
}
/**
* CURLHandler::Post()
*
* Executes a standard POST request via Curl.
* Static function, so you can use CurlHandler::Post('http://www.google.com', array('q'=>'StackOverFlow'));
* If you want to send a File via post (to e.g. PHP's $_FILES), prefix the value of an item with an @ !
* @param string $url url to post data to
* @param Array $vars Array with key=>value pairs to post.
* @return string HTML output
*/
public static function Post($url, $vars, $auth = false)
{
return self::doRequest('POST', $url, $vars, $auth);
}
/**
* CURLHandler::doRequest()
* This is what actually does the request
* <pre>
* - Create Curl handle with curl_init
* - Set options like CURLOPT_URL, CURLOPT_RETURNTRANSFER and CURLOPT_HEADER
* - Set eventual optional options (like CURLOPT_POST and CURLOPT_POSTFIELDS)
* - Call curl_exec on the interface
* - Close the connection
* - Return the result or throw an exception.
* </pre>
* @param mixed $method Request Method (Get/ Post)
* @param mixed $url URI to get or post to
* @param mixed $vars Array of variables (only mandatory in POST requests)
* @return string HTML output
*/
public static function doRequest($method, $url, $vars=array(), $auth = false)
{
$curlInterface = curl_init();
curl_setopt_array ($curlInterface, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION =>1,
CURLOPT_HEADER => 0));
if (strtoupper($method) == 'POST')
{
curl_setopt_array($curlInterface, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($vars))
);
}
if($auth !== false)
{
curl_setopt($curlInterface, CURLOPT_USERPWD, $auth['username'] . ":" . $auth['password']);
}
$result = curl_exec ($curlInterface);
curl_close ($curlInterface);
if($result === NULL)
{
throw new Exception('Curl Request Error: '.curl_errno($curlInterface) . " - " . curl_error($curlInterface));
}
else
{
return($result);
}
}
}
?>
[编辑]阅读澄清只是现在......你可能想要去的上面自动东西提到的工具之一。你也可以决定使用一个客户方Firefox扩展如奇肯富特了解灵活性。我会离开的例子类以上供将来搜索。
如果你在你的项目使用CakePHP,或者如果你倾向于提取您可以使用自己的卷曲包装HttpSocket相关库。它具有简单的页面撷取语法你描述,例如
# This is the sugar for importing the library within CakePHP
App::import('Core', 'HttpSocket');
$HttpSocket = new HttpSocket();
$result = $HttpSocket->post($login_url,
array(
"username" => "username",
"password" => "password"
)
);
...虽然它没有一种方法来解析响应页面。对于我将使用simplehtmldom:的 http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/ 其描述本身为具有一个jQuery的语法。
我倾向于同意底线是PHP不具有的Perl /红宝石具有真棒刮削/自动化库。
如果您是在* nix系统上,你可以使用带有wget的,里面有很多不错的选择了shell_exec()。