zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    Python网络编程(weekly summary1)
    Python网络编程(子进程的创建与处理、简单群聊工具)
    Python网络编程(epoll内核监听,多任务多进程)
    Python网络编程(http协议,IO多路复用、select内核监听)
    Python网络编程(socket模块、缓冲区、http协议)
    Python网络编程(OSI模型、网络协议、TCP)
    Linux(Ubuntu 命令大全)
    计算机中的进制和编码
    算法图解(选择排序)
    Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)
  • 原文地址:https://www.cnblogs.com/yxbs/p/3312679.html
Copyright © 2011-2022 走看看