curl命令 发表于 2018-08-06 命令行操作12345#getcurl http://localhost/index.php?a=1&b=2#postcurl -d "a=1&b=2" http://localhost/index.php php扩展操作get请求123456789101112//创建curl资源$curl = curl_init();//设定url及相应选项curl_setopt($curl, CURLOPT_URL, $url); //设定访问地址curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //1 将获取的信息以文件流形式返回 0直接输出//抓取内容并返回$contents = curl_exec($curl);//关闭资源curl_close($curl); post请求123456789$curl = curl_init();$post = 'name=lilei&age=18';curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_POST, 1); //设定为POST请求curl_setopt($curl, CURLOPT_POSTFIELDS, $post); //POST数据$contents = curl_exec($curl); cookie操作123456789101112131415161718192021222324252627282930313233343536//获取cookie$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);$contents = curl_exec($curl); //返回抓取的内容----------------------------------------------------------//使用已有cookie$curl = curl_init();$timeout = 5;$cookie_file = "./tmp.cookie";curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //使用cookie文件$contents = curl_exec($curl);-----------------------------------------------------------//使用已有cookie并存储 (建议直接使用这种写法,以免漏掉某些关键cookie信息)$curl = curl_init();$timeout = 5;$cookie_file = "./tmp.cookie";curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //使用cookie文件curl_setopt($curl, CURLOPT_COOKIEFILE,$cookie_file); //存储COOKIE $contents = curl_exec($curl); https请求12345678910111213141516171819202122232425$cacert = getcwd() . '/cacert.pem'; //CA根证书 $SSL = substr($url, 0, 8) == "https://" ? true : false; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout-2); if ($SSL && $CA) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书 curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配 } else if ($SSL && !$CA) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名 } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode $ret = curl_exec($ch); //var_dump(curl_error($ch)); //查看报错信息 curl_close($ch); 应用模拟验证码登陆原地址 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960/** * 模拟登录 *///初始化变量$cookie_file = "tmp.cookie";$login_url = "http://xxx.com/logon.php";$verify_code_url = "http://xxx.com/verifyCode.php";echo "正在获取COOKIE...\n";$curlj = curl_init();$timeout = 5;curl_setopt($curl, CURLOPT_URL, $login_url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);curl_setopt($curl, CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储$contents = curl_exec($curl);curl_close($curl);echo "COOKIE获取完成,正在取验证码...\n";//取出验证码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $verify_code_url);curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);curl_setopt($curl, CURLOPT_HEADER, 0);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$img = curl_exec($curl);curl_close($curl);$fp = fopen("verifyCode.jpg","w");fwrite($fp,$img);fclose($fp);echo "验证码取出完成,正在休眠,20秒内请把验证码填入code.txt并保存\n";//停止运行20秒sleep(20);echo "休眠完成,开始取验证码...\n";$code = file_get_contents("code.txt");echo "验证码成功取出:$code\n";echo "正在准备模拟登录...\n";$post = "username=maben&pwd=hahahaha&verifycode=$code";$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_HEADER, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);curl_setopt($curl, CURLOPT_POSTFIELDS, $post);curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);$result=curl_exec($curl);curl_close($curl);//这一块根据自己抓包获取到的网站上的数据来做判断if(substr_count($result,"登录成功")){ echo "登录成功\n";}else{ echo "登录失败\n"; exit;}//OK,开始做你想做的事吧。。。。。 资料官方文档