curl post登录 抓取重定向网址

现在网站都做得贼拉恶心,boss要做个抓取程序,别人的网站要求登录,登录入口就一个,用curl写了个,死活不行,抓包一看才发下丫的做了n次跳转,无耻呀~~这不是逼我嘛~~招了n就没找到现成的~~唉自个动手吧~~~存个档

<?php

set_time_limit(1000); 

$timeout = 1000;   //超时时间
$cookie_jar = tempnam('./','cookie');//cookie

function curl_redirect_exec($ch, &$redirects, $curlopt_returntransfer = false, $curlopt_maxredirs = 10, $curlopt_header = false) {
 curl_setopt($ch, CURLOPT_HEADER, true);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $data = curl_exec($ch);
 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $exceeded_max_redirects = $curlopt_maxredirs > $redirects;
 $exist_more_redirects = false;
 if ($http_code == 301 || $http_code == 302) {
  if ($exceeded_max_redirects) {
   list($header) = explode("\r\n\r\n", $data, 2);
   $matches = array();
   preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
   $url = trim(array_pop($matches));
   $url_parsed = parse_url($url);
    if (isset($url_parsed)) {
    curl_setopt($ch, CURLOPT_URL, $url);
    $redirects++;
    return curl_redirect_exec($ch, $redirects, $curlopt_returntransfer, $curlopt_maxredirs, $curlopt_header);
    }
  } else {
    $exist_more_redirects = true;
    }
 }
 if ($data !== false) {
  if (!$curlopt_header)
   list(,$data) = explode("\r\n\r\n", $data, 2);
  if ($exist_more_redirects) return false;
  if ($curlopt_returntransfer) {
    return $data;
   } else {
    if (curl_errno($ch) === 0) return true;
    else return false;
     }
 } else {
 return false;
 }
}

$useragent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)";

$request="user=user&pwd=pwd";
$loginurl  ="login.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_REFERER, 'index.asp');
curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_NOBODY,  1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_exec($ch);  
$info = curl_getinfo($ch);
curl_close($ch);

@unlink($cookie_jar);
  ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无风自舞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值