brunodebarros / http-request
一个易于集成到PHP应用程序中的网络爬虫,其行为类似于常规网络浏览器,自动解释位置重定向并存储cookies。
1.0.2
2015-04-06 15:58 UTC
Requires
- php: >=5.2.4
This package is auto-updated.
Last update: 2024-08-29 03:10:44 UTC
README
HTTP Request 1.0
HTTP Request是一个网络爬虫,其行为类似于常规网络浏览器,自动解释位置重定向并存储cookies。
功能
- HTTP/HTTPS请求
- 自动处理cookies和重定向
- GET/POST请求
如何使用
使用Composer安装
将brunodebarros/http-request
添加到您的composer.json文件内容中
{
"require": {
"brunodebarros/http-request": "dev-master"
}
}
然后使用它!
$http = new HTTP_Request();
$content = $http->request($url, $mode, $data, $save_to_file);
$url是要访问的URL。HTTP Request支持HTTPS请求,就像HTTP一样简单,只要您在PHP服务器上启用了OpenSSL。
$mode是GET或POST。如果为空,将执行GET请求。
$data是通过GET或POST传递的数据数组。很简单。如果为空,请求中不会发送任何额外数据。
$save_to_file是要存储输出的文件名。如果为空,输出将不会存储在任何地方。
示例
$http = new HTTP_Request();
# Make a simple GET Request:
$content = $http->request('http://website.com');
# Make a POST request to a HTTPS website with some data:
$content = $http->request('https://website.com/login', 'POST', array('user' => 'myusername', 'pass' => 'mypassword'));
# Make a simple GET Request and store it in a file:
$http->request('http://website.com', 'GET', array(), 'contents_of_website_dot_com.txt')
为什么自动处理cookies很棒
HTTP Request最棒的部分是它会自动处理位置重定向和cookies。因此,如果您将登录详细信息POST到网站登录页面,然后访问该网站上的另一个页面,网站将相信您已登录,因为HTTP Request已经保留了cookies。这样,您可以构建真正的类似人类的网络爬虫,轻松执行几乎任何人类可以执行的操作。
$http = new HTTP_Request();
# Login to website.com
$content = $http->request('https://website.com/login', 'POST', array('user' => 'myusername', 'pass' => 'mypassword'));
# Access restricted page (because of cookie handling, you will be logged in when you request this page, without any effort on your part)
$http->request('http://website.com/page-for-logged-in-users-only');
即将推出。
我打算使HTTP Request变得非常强大。以下是我的一些想法
- 增强cookies处理(当服务器发送请求删除cookies时删除cookies,将cookies存储在文件中以备将来访问等。)
- 增强重定向解释(使其与HTML meta重定向和JavaScript重定向一起工作)
- 分块下载,以允许下载非常大的文件而不会超出PHP的memory_limit。
- 如果您有任何想法,请创建一个问题。如果您创建一个问题,我将尽一切努力解决它。
建议、问题和建议。
如果您有任何建议、问题或对HTTP Request不喜欢的地方,请创建一个问题。我将非常感谢您的反馈。如果您想为此项目做出贡献,请随意将其分支。