PHP读取CURL模拟登录时生成Cookie文件的方法
时间:2020-10-06 21:56:06|栏目:PHP代码|点击: 次
本文实例讲述了PHP读取CURL模拟登录时生成Cookie文件的方法。分享给大家供大家参考。具体实现方法如下:
在使用PHP中的CURL模拟登录时会保存一个Cookie文件,例如下面的代码
复制代码 代码如下:
$login_url = 'XXX';
$post_fields['email'] = 'XXXX';
$post_fields['password'] = 'XXXX';
$post_fields['origURL'] = 'XXX';
$post_fields['domain'] = 'xxx.com';
//cookie文件存放在网站根目录的temp文件夹下
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
//带上cookie文件,访问需要访问的页面
$send_url='xxx.com';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);
//输出网页内容
print_r($contents);
$post_fields['email'] = 'XXXX';
$post_fields['password'] = 'XXXX';
$post_fields['origURL'] = 'XXX';
$post_fields['domain'] = 'xxx.com';
//cookie文件存放在网站根目录的temp文件夹下
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
//带上cookie文件,访问需要访问的页面
$send_url='xxx.com';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);
//输出网页内容
print_r($contents);
在temp文件夹下保存一个cookie前缀的临时文件,例如:coo3A98.tmp文件
打开这个文件得到如下代码:
要使用php来格式化该文件,使用以下代码就能实现
复制代码 代码如下:
<?php
$cookie_folder = dirname(__FILE__)."/temp";
$lines = file($cookie_folder.'/coo3A98.tmp');
$trows = '';
foreach($lines as $line) {
if($line[0] != '#' && substr_count($line, "\t") == 6) {
$tokens = explode("\t", $line);
$tokens = array_map('trim', $tokens);
$tokens[4] = date('Y-m-d h:i:s', $tokens[4]);
$trows .= '<tr><td>' . implode('</td><td>', $tokens) . '</td></tr>' . PHP_EOL;
}
}
echo '<table>'.PHP_EOL.'<tbody>'.PHP_EOL.$trows.'</tbody>'.PHP_EOL.'</table>';
?>
$cookie_folder = dirname(__FILE__)."/temp";
$lines = file($cookie_folder.'/coo3A98.tmp');
$trows = '';
foreach($lines as $line) {
if($line[0] != '#' && substr_count($line, "\t") == 6) {
$tokens = explode("\t", $line);
$tokens = array_map('trim', $tokens);
$tokens[4] = date('Y-m-d h:i:s', $tokens[4]);
$trows .= '<tr><td>' . implode('</td><td>', $tokens) . '</td></tr>' . PHP_EOL;
}
}
echo '<table>'.PHP_EOL.'<tbody>'.PHP_EOL.$trows.'</tbody>'.PHP_EOL.'</table>';
?>
运行之后就如下图所示,已经被写入到table当中
大功告成,如果只读取其中字段可自行修改即可。
希望本文所述对大家的PHP程序设计有所帮助。