是否有一个Perl的WWW :: Mechanize的PHP等价物?

问题描述:

我正在寻找一个功能类似于Perl的WWW::Mechanize但对于PHP的库。基本上,它应该允许我用简单的语法提交HTTP GET和POST请求,然后解析生成的页面并以简单的格式返回所有表单及其字段以及页面上的所有链接。是否有一个Perl的WWW :: Mechanize的PHP等价物?

我知道卷曲,但它是一个有点准系统和语法是非常难看(的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或curl将是一个很大的工作做同样的事情,我不得不手工解析ŧ他会寻找链接,找到表单URL,提取所有隐藏的字段,等等。我要求一个PHP解决方案的原因是,我没有使用Perl的经验,而且我可能通过很多工作来构建我需要的东西,但是如果我可以在PHP中完成上述任务,则会更快。

+0

实际上*是*的一个端口:http://www.compasswebpublisher.com/php/www-mechanize-for-php但它是一些CMS的一部分,并且没有下载。 – Gordon 2013-03-06 08:34:43

SimpleTest的的ScriptableBrowser可以从测试框架independendly使用。我已经将它用于众多自动化作业。

尝试寻找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); 
    } 
} 
+0

这不是我正在寻找的,我添加了一个澄清,希望更清楚,谢谢。 – davr 2008-10-13 23:21:57

请尝试以下之一:

(是的,这是ZendFramework代码,但它不会让你的班级慢慢使用它,因为它只需加载所需的库)。

+0

他们仍然比机械化工作多得多,请看我对这个问题的澄清。 – davr 2008-10-13 23:10:30

+0

好Q.我认为他们都没有那样做。但我认为我会为建设而努力,明天我会看看机械化API。 – Till 2008-10-14 13:13:28

+0

如果你最终做出任何事情,请发表一个新的答案,我一定会看看它。您可能希望将'ScriptableBrowser'的答案看作一个起点,我认为它只需要更多的功能来完成Mechanize所做的一切。 – davr 2008-10-14 20:43:12

如果你在* nix系统上,你可以使用shell_exec()和wget,它有很多不错的选择。

+1

噢,好吧,我不会把用户输入放在那里。 – 2008-10-15 13:56:31

+0

看起来很有趣,但它很老旧(2005年的最后一次更新),虽然比curl/wget更好,但缺少一些可以使它更好的功能。 – davr 2008-10-13 23:20:20

Curl是简单请求的方式。它运行跨平台,具有PHP扩展并被广泛采用和测试。

我创建了一个很好的类,它可以通过调用CurlHandler :: Get($ url,$ data)来GET和POST一个数据数组(包括文件! CurlHandler :: Post($ url,$ data)。还有一个可选的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'=>'*')); 
    * 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); 
     } 
    } 

} 

?> 

[编辑]阅读澄清只是现在......你可能想要去的上面自动东西提到的工具之一。您也可以决定使用像ChickenFoot这样的客户端Firefox扩展来获得更大的灵活性。我将在上面留下示例类以供将来搜索。

我觉得不得不回答这个问题,尽管它的旧帖子...我一直在使用PHP curl很多,它不像WWW那样好:机械化,我正在切换到(我认为我会去用Ruby语言实现)。Curl已经过时了因为它需要太多的“笨重的工作”来自动化任何事情,最简单的脚本化浏览器看起来很有希望,但在测试它时,它不适用于我试用的大多数Web表单...老实说,我认为PHP缺乏这类刮板,网页自动化,所以它最好看看不同的语言,只是想发布这个,因为我已经花了无数小时在这个话题上,也许它会在未来的某个时间保存别人。

如果您在您的项目中使用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/Ruby所具有的真棒抓取/自动化库。

现在是2016年,现在有Mink。它甚至支持来自无头的纯PHP“浏览器”(无JavaScript)的不同引擎,而不是Selenium(需要Firefox或Chrome等浏览器)到NPM中的无头“browser.js”,它支持JavaScript。