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);

在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>';
?>

运行之后就如下图所示,已经被写入到table当中


大功告成,如果只读取其中字段可自行修改即可.

更多精彩博文请到言会咸的博客

网址:http://blog.csdn.net/aoyoo111

发布了37 篇原创文章 · 获赞 4 · 访问量 58万+
展开阅读全文

使用cURL PHP从页面获取所有cookie

06-11

<div class="post-text" itemprop="text"> <p><img src="https://i.stack.imgur.com/yuwcs.jpg" alt="enter image description here"> I'm trying to access a page using PHP's cURL, this page is to return 2 cookies in the "Response Headers", but in the cURL header only returns the first one. </p> <p>Does anyone know if there might be some block on the page, and how to work around this problem?</p> <blockquote> <p>This cookie is generated on a "Status Code: 302"</p> </blockquote> <p>I've already tried to get "file_get_contents" and also tried to do the <code>"CURLOPT_HEADERFUNCTION"</code> function and I also know success OBS: The function to break the google captcha is already working.</p> <pre><code>$url = 'https://pje.trt15.jus.br/captcha/login_post.php'; $post = [ 'g-recaptcha-response' => $g_response, 'referer' => '/consultaprocessual/pages/consultas/ConsultaProcessual.seam', 'random' => $g_captcha_random, 'entrar' => $g_captcha_entrar ]; if( $g_captcha_enviar != null ) $post[$g_captcha_enviar] = 'Enviar'; if( $g_captcha_entrar != null ) $post['entrar'] = $g_captcha_entrar; $post_http = http_build_query($post); $headers = [ ':authority: pje.trt15.jus.br', ':method: POST', ':path: /captcha/login_post.php', ':scheme: https', 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', //'accept-encoding: gzip, deflate, br', 'accept-language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7', 'cache-control: no-cache', 'content-length: ' . strlen($post_http), 'content-type: application/x-www-form-urlencoded', 'cookie: '.$cookie_captchasess.' _ga=GA1.3.1830456077.1560269951; _gid=GA1.3.249690674.1560269951', 'origin: https://pje.trt15.jus.br', 'referer: https://pje.trt15.jus.br/consultaprocessual/pages/consultas/ConsultaProcessual.seam', 'upgrade-insecure-requests: 1' ]; $options = [ CURLOPT_COOKIESESSION => true, CURLOPT_HEADER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $post_http, CURLOPT_HTTPHEADER => $headers, CURLOPT_COOKIE => $cookie_captchasess, CURLOPT_FRESH_CONNECT => true, CURLOPT_VERBOSE => true, CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS, CURLOPT_ENCODING => 'gzip, deflate', CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_AUTOREFERER => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 40, CURLOPT_USERAGENT => $user_agent, ]; $ch = curl_init(); curl_setopt_array($ch, $options); $resp = curl_exec($ch); curl_close($ch); echo "<pre>"; echo $resp; </code></pre> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览