PS :请放心,这不会将用于垃圾邮件.解决方案请参见 http://code.google.com/p/phpquery/wiki/Ajax ,尤其是: phpQuery::post($url, $data, $callback, $type) 和 # data Object, String将数据参数定义为对象或字符串. POST请求应该使用查询字符串格式,例如:$data = "username=Jon&password=123456";$url = "http://www.mysite.com/login.php";phpQuery::post($url, $data, $callback, $type)由于phpQuery是jQuery端口,因此方法签名是相同的(文档直接链接到jquery站点- http://docs.jquery.com/Ajax/jQuery.post ) 修改 两件事:还有一个 phpQuery::browserPost 函数可能会更好地满足您的需求.但是,还请注意,仅在 submit()或click() 方法,因此您可以在此之前填写所有表单字段.例如require_once('phpQuery/phpQuery.php');phpQuery::browserGet('http://www.mysite.com/login.php', 'success1');function success1($browser) { $handle = $browser ->WebBrowser('success2'); $handle ->find('input[name=username]') ->val('Jon'); $handle ->find('input[name=password]') ->val('123456'); ->parents('form') ->submit();}function success2($browser) { print $browser;}(请注意,此功能尚未经过测试,但应该有效)I'm looking for a PHP library that allows me to scrap webpages and takes care about all the cookies and prefilling the forms with the default values, that's what annoys me the most.I'm tired of having to match every single input element with xpath and I would love if something better existed. I've come across phpQuery but the manual isn't much clear and I can't find out how to make POST requests.Can someone help me? Thanks.@Jonathan Fingland:In the example provided by the manual for browserGet() we have:require_once('phpQuery/phpQuery.php');phpQuery::browserGet('http://google.com/', 'success1');function success1($browser){ $browser->WebBrowser('success2') ->find('input[name=q]')->val('search phrase') ->parents('form') ->submit();}function success2($browser){ echo $browser;}I suppose all the other fields are scrapped and send back in the GET request, I want to do the same with the phpQuery::browserPost() method but I don't know how to do it. The form I'm trying to scrape has a input token and I would love if phpQuery could be smart enough to scrape the token and just let me change the other fields (in this case username and password), submiting via POST everything.PS: Rest assured, this is not going to be used for spamming. 解决方案 See http://code.google.com/p/phpquery/wiki/Ajax and in particular:phpQuery::post($url, $data, $callback, $type)and# data Object, String which defines the data parameter as being either an Object or a String. POST requests should be possible using query string format, e.g.:$data = "username=Jon&password=123456";$url = "http://www.mysite.com/login.php";phpQuery::post($url, $data, $callback, $type)as phpQuery is a jQuery port the method signature is the same (the docs link directly to the jquery site -- http://docs.jquery.com/Ajax/jQuery.post)EditTwo things:There is also a phpQuery::browserPost function which might meet your needs better.However, also note that the success2 callback is only called on the submit() or click() methods so you can fill in all of the form fields prior to that.e.g.require_once('phpQuery/phpQuery.php');phpQuery::browserGet('http://www.mysite.com/login.php', 'success1');function success1($browser) { $handle = $browser ->WebBrowser('success2'); $handle ->find('input[name=username]') ->val('Jon'); $handle ->find('input[name=password]') ->val('123456'); ->parents('form') ->submit();}function success2($browser) { print $browser;}(Note that this has not been tested, but should work) 这篇关于用于PHP的报废库-phpQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 09:34