Php 是否存在与Perl';机械化?
我正在寻找一个库,它的功能类似于Perl,但适用于PHP。基本上,它应该允许我使用简单的语法提交HTTPGET和POST请求,然后解析生成的页面并以简单的格式返回所有表单及其字段,以及页面上的所有链接 我知道CURL,但它有点过于简单,语法也非常难看(大量的Php 是否存在与Perl';机械化?,php,automation,screen-scraping,mechanize,www-mechanize,Php,Automation,Screen Scraping,Mechanize,Www Mechanize,我正在寻找一个库,它的功能类似于Perl,但适用于PHP。基本上,它应该允许我使用简单的语法提交HTTPGET和POST请求,然后解析生成的页面并以简单的格式返回所有表单及其字段,以及页面上的所有链接 我知道CURL,但它有点过于简单,语法也非常难看(大量的CURL\u foo($CURL\u handle,…)语句 澄清: 我想要比目前的答案更高层次的东西。例如,在Perl中,您可以执行以下操作: # navigate to the main page $mech->get( 'http
CURL\u foo($CURL\u 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_客户端、wget或CURL做同样的事情需要大量的工作,我必须手动解析页面以找到链接、找到表单URL、提取所有隐藏字段,等等。我要求使用PHP解决方案的原因是我没有Perl方面的经验,我可能需要做大量的工作来构建我需要的内容,但它会如果我能在PHP中完成上述操作,我会更快。尝试查找PEAR库。如果所有其他操作都失败,请为curl创建一个对象包装器 你可以做一些简单的事情,比如:
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);
}
}
请尝试以下操作之一:
- 梨的
(是的,这是ZendFramework代码,但使用它不会让类变慢,因为它只加载所需的libs。)如果您使用的是*nix系统,您可以将shell_exec()与wget一起使用,wget有很多不错的选项。看看Snoopy:
SimpleTest可以独立于测试框架使用。我已经将它用于许多自动化工作。Curl是处理简单请求的方法。它跨平台运行,具有PHP扩展,并且被广泛采用和测试 我创建了一个很好的类,只需调用CurlHandler::GET($url,$data)| | CurlHandler::POST($url,$data),就可以获取数据数组(包括文件!)并将其发布到url。还有一个可选的HTTP用户身份验证选项:)
/**
*CURLHandler通过Curl处理简单的HTTP GET和POST
*
*@包猪肉
*@author-SchizoDuckie
*@版权所有SchizoDuckie 2008
*@version 1.0
*@access-public
*/
类卷曲处理程序
{
/**
*CURLHandler::Get()
*
*通过Curl执行标准GET请求。
*静态函数,以便可以使用:CurlHandler::Get('http://www.google.com');
*
*@param string$url要获取的url
*@返回字符串HTML输出
*/
公共静态函数Get($url)
{
返回self::doRequest('GET',$url);
}
/**
*CURLHandler::Post()
*
*通过Curl执行标准POST请求。
*静态函数,因此可以使用CurlHandler::Post('http://www.google.com,数组('q'=>'StackOverFlow');
*如果要通过post发送文件(例如PHP的$_文件),请在项目的值前面加@!
*@param string$url用于向其发布数据
*@param Array$vars Array,带有要发布的键=>值对。
*@返回字符串HTML输出
*/
公共静态函数Post($url、$vars、$auth=false)
{
返回self::doRequest('POST',$url,$vars,$auth);
}
/**
*CURLHandler::doRequest()
*这就是请求的实际用途
*
*-使用Curl_init创建Curl句柄
*-设置CURLOPT_URL、CURLOPT_RETURNTRANSFER和CURLOPT_标头等选项
*-设置最终可选选项(如CURLOPT_POST和CURLOPT_POSTFIELDS)
*-在接口上调用curl_exec
*-关闭连接
*-返回结果或引发异常。
*
*@param混合$method请求方法(Get/Post)
*@param要获取或发布到的混合$url URI
*@param混合$vars变量数组(仅在POST请求中是必需的)
*@返回字符串HTML输出
*/
公共静态函数doRequest($method,$url,$vars=array(),$auth=false)
{
$curlInterface=curl_init();
curl_setopt_数组($curl接口,数组(
CURLOPT_URL=>$URL,
CURLOPT_RETURNTRANSFER=>1,
CURLOPT_FOLLOWLOCATION=>1,
CURLOPT_头=>0);
if(strtoupper($method)=‘POST’)
{
curl_setopt_数组($curl接口,数组(
CURLOPT_POST=>1,
CURLOPT_POSTFIELDS=>http_build_query($vars))
);
}
如果($auth!==false)
{
curl_setopt($curlInterface,CURLOPT_USERPWD,$auth['username']。”:“$auth['password']);
}
$result=curl\u exec($curl接口);
curl_close($curl接口);
如果($result==NULL)
{
抛出新异常('Curl请求错误:'.Curl_errno($curlInterface)。“-”.Curl_Error($curlInterface));
}
其他的
{
返回($结果);
}
}
}
?>
[编辑]现在才阅读澄清。。。你可能想使用上面提到的自动化工具之一。您还可以决定使用客户端firefox扩展,例如,以获得更大的灵活性。我将把上面的示例类留在这里,供将来搜索。我觉得有必要回答这个问题,尽管这是一篇旧文章。。。我一直在使用PHP curl,但它远不如我正在使用的WWW:Mechanize(我想我将使用Ruby语言实现)那样好。。Curl已经过时了,因为它需要太多的“繁重工作”来自动化任何东西,simpletest脚本浏览器对我来说很有希望,但在测试中,它在我尝试的大多数web表单上都不起作用。。。老实说,我认为PHP在这一类的抓取、web自动化中是缺乏的,所以最好看一种不同的语言,我只是想发表这篇文章,因为我已经在这个主题上花费了无数个小时,也许这会在将来为其他人节省一些时间。如果你在项目中使用CakePHP,或者,如果您倾向于提取相关的库,您可以使用
/**
* 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);
}
}
}
?>
# 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"
)
);