php cUrl模拟登录并下载文档
2020/10/23 13:39
教程
<?php header("Content-Type:text/html;charset=utf-8"); //获取cookie $UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'; $login_url = 'https://xxxxxxx'; //登录页面地址 $cookie_file = dirname(__FILE__)."/tmp.cookie"; //cookie文件存放位置 $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent); curl_setopt($ch, CURLOPT_URL, $login_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_exec($ch); curl_close($ch); //模拟登录 $post_url = 'https://xxxxxx'; //登录表单提交地址 $post = 'username=用户名&password=密码';//表单提交的数据(根据表单字段名和用户输入决定) $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent); curl_setopt($ch, CURLOPT_URL, $post_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //post提交数据 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //post提交数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $post); //提交方式为post curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_exec($ch); curl_close($ch); //下载文件 $url = "'https://xxxxxx'; //下载链接 $arr = curl_get($url, $cookie_file); function curl_get($url, $cookie_file) { $curl2 = curl_init(); curl_setopt($curl2, CURLOPT_USERAGENT, $UserAgent); curl_setopt($curl2, CURLOPT_URL, $url);//登陆后要从哪个页面获取信息 curl_setopt($curl2, CURLOPT_HEADER, false); curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl2, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl2, CURLOPT_COOKIEFILE, $cookie_file); $content = curl_exec($curl2); $date = date('Ymd'); file_put_contents("$date.xls", $content); if(curl_exec($curl2) === false) { echo 'Curl error: ' . curl_error($curl2); } else { echo '下载完成'; } curl_close($curl2); }
留言评论