我的回答仅适用于表单身份验证(这是最常见的身份验证形式)。
基本上,当您浏览一个网站时,您会在其上打开一个“会话”。当您在网站上登录时,您的会话会得到“身份验证”,并且基于此您被授予任何地方的访问权限。
由于存储在 cookie 中的会话 ID,您的浏览器可以识别到服务器的相应会话。
所以你必须先浏览登录页面,然后再浏览你想要的页面,过程中不要忘记发送cookie。cookie 是您浏览的所有页面之间的链接。
我实际上遇到了你不久前遇到的同样的问题,并且写了一个类来做到这一点,而不必记住这个 cookie 的东西。
快看类,不重要,但看好下面的例子。它允许您提交实现 CSRF 保护的表单。
此类基本上具有以下功能: - 符合基于 CSRF 令牌的保护 - 发送“通用”用户代理。一些网站拒绝不与用户代理通信的查询 - 发送推荐人标头。一些网站拒绝不与推荐人通信的查询(这是另一种反 csrf 保护) - 在调用中存储 cookie
文件:WebClient.php
<?php
/**
* Webclient
*
* Helper class to browse the web
*
* @author Bgi
*/
class WebClient
{
private $ch;
private $cookie = '';
private $html;
public function Navigate($url, $post = array())
{
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_COOKIE, $this->cookie);
if (!empty($post)) {
curl_setopt($this->ch, CURLOPT_POST, TRUE);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
}
$response = $this->exec();
if ($response['Code'] !== 200) {
return FALSE;
}
//echo curl_getinfo($this->ch, CURLINFO_HEADER_OUT);
return $response['Html'];
}
public function getInputs()
{
$return = array();
$dom = new DOMDocument();
@$dom->loadHtml($this->html);
$inputs = $dom->getElementsByTagName('input');
foreach($inputs as $input)
{
if ($input->hasAttributes() && $input->attributes->getNamedItem('name') !== NULL)
{
if ($input->attributes->getNamedItem('value') !== NULL)
$return[$input->attributes->getNamedItem('name')->value] = $input->attributes->getNamedItem('value')->value;
else
$return[$input->attributes->getNamedItem('name')->value] = NULL;
}
}
return $return;
}
public function __construct()
{
$this->init();
}
public function __destruct()
{
$this->close();
}
private function init()
{
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1");
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($this->ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($this->ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($this->ch, CURLOPT_HEADER, TRUE);
curl_setopt($this->ch, CURLOPT_AUTOREFERER, TRUE);
}
private function exec()
{
$headers = array();
$html = '';
ob_start();
curl_exec($this->ch);
$output = ob_get_contents();
ob_end_clean();
$retcode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
if ($retcode == 200) {
$separator = strpos($output, "\r\n\r\n");
$html = substr($output, $separator);
$h = trim(substr($output,0,$separator));
$lines = explode("\n", $h);
foreach($lines as $line) {
$kv = explode(':',$line);
if (count($kv) == 2) {
$k = trim($kv[0]);
$v = trim($kv[1]);
$headers[$k] = $v;
}
}
}
// TODO: it would deserve to be tested extensively.
if (!empty($headers['Set-Cookie']))
$this->cookie = $headers['Set-Cookie'];
$this->html = $html;
return array('Code' => $retcode, 'Headers' => $headers, 'Html' => $html);
}
private function close()
{
curl_close($this->ch);
}
}
我该如何使用它?
在这个例子中,我登录到一个网站,然后浏览到一个包含上传文件的表单的页面,然后我上传文件:
<?php
require_once('WebClient.php');
$url = 'http://example.com/administrator/index.php'; // This a Joomla admin
$wc = new WebClient();
$page = $wc->Navigate($url);
if ($page === FALSE) {
die('Failed to load login page.');
}
echo('Logging in...');
$post = $wc->getInputs();
$post['username'] = $username;
$post['passwd'] = $passwd;
$page = $wc->Navigate($url, $post);
if ($page === FALSE) {
die('Failed to post credentials.');
}
echo('Initializing installation...');
$page = $wc->Navigate($url.'?option=com_installer');
if ($page === FALSE) {
die('Failed to access installer.');
}
echo('Installing...');
$post = $wc->getInputs();
$post['install_package'] = '@'.$file; // The @ specifies we are sending a file
$page = $wc->Navigate($url.'?option=com_installer&view=install', $post);
if ($page === FALSE) {
die('Failed to upload file.');
}
echo('Done.');
Navigate() 方法返回 FALSE 或浏览页面的 HTML 内容。
哦,还有最后一件事:不要使用正则表达式来解析 HTML,这是错误的。对此有一个传奇的 StackOverflow 答案:请参见此处。