• PHP CURL 登陆


    <?php
    $testURL = 'http://www.test.com/show.asp?id=123';
    $UserURL = 'http://www.test.com/userlogin.asp?action=login';
    $UserData = array('username'=>'coldstar','password'=>'123456.');
    $cookiepath = $_SERVER["DOCUMENT_ROOT"] .'\' .MD5($UserURL);	//以主域名的MD5值设置为COOKIE文件名
    $html = curl_post_contents($UserURL,$UserData,$cookiepath);	//模拟登陆
    if($html){
    	if(stripos($html,'登陆成功')){
    		$html = curl_get_contents($testURL,True,$cookiepath);	//获取真正的内容
    	}else{
    		$html = '登陆失败';
    	}
    }
    echo $html;
    
    
    function curl_get_contents($url,$usecookie = 0,$cookiepath = ''){
    	$userAgent = 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)';
    	$referer = $url;
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);				//设置访问的url地址
    	curl_setopt($ch, CURLOPT_TIMEOUT, 10);				//设置超时
    	curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);	//用户访问代理 User-Agent
    	curl_setopt($ch, CURLOPT_REFERER, $referer);		//设置 referer
    	if($usecookie){
    		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiepath);	//COOKIE的存储路径,传送时使用
    	}
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);		//跟踪301
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		//返回结果
    	$r = curl_exec($ch);
    	curl_close($ch);
    	return $r;
    }
    
    function curl_post_contents($url,$data = array(),$cookiepath = ''){
    	$userAgent = 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)';
    	$referer = $url;
    	if(!is_array($data) || !$url) return '';
    	foreach($data as $key=>$value){$post .= urlencode($key).'='.$value.'&';}
    	rtrim($post ,'&');
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);				//设置访问的url地址
    	curl_setopt($ch, CURLOPT_TIMEOUT, 10);				//设置超时
    	curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);	//用户访问代理 User-Agent
    	curl_setopt($ch, CURLOPT_REFERER, $referer);		//设置 referer
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);		//跟踪301
    	curl_setopt($ch, CURLOPT_POST, 1);					//指定post数据
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);		//添加变量
    	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiepath);	//COOKIE的存储路径,返回时保存COOKIE的路径
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		//返回结果
    	$r = curl_exec($ch);
    	curl_close($ch);
    	return $r;
    }
    ?>
    

      转自:http://www.yanghengfei.com/archives/415/

  • 相关阅读:
    C++ 概念易错点
    C++的位操作符备忘
    C++关键词
    在ubuntu下安装drupal6
    C++符号优先级一览
    开启drupal的clear urls
    VC6.0使用PlaySound函数报错
    小记一下以非string为结束条件的循环
    C++中查看数据类型的方法
    在ubuntu下安装和配置drupal
  • 原文地址:https://www.cnblogs.com/yxbs/p/3312679.html
Copyright © 2020-2023  润新知