这篇文章主要为大家详细介绍了PHP利用CURL获取邮箱地址示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
CURL可谓居家旅行必备之杀人良药,为何如此形容?就是因为他好用方便能实现页面抓取模拟登录采集等一系列功能。代码如下:
<?php
/* 四海网 www.q1010.com */
error_reporting ( 0 );
set_time_limit ( 0 );
header ( "Content-Type: text/html; charset=GB2312" );
//邮箱用户名密码
$user = 'username';
$pass = 'password';
//创建一个文件用于存放cookie信息
define ( "COOKIEJAR", tempnam ( ini_get ( "upload_tmp_dir" ), "cookie" ) );
$url = 'http://reg.163.com/logins.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1';
$refer = 'http://mail.163.com';
$fields_post = array ('username' => $user, 'password' => $pass, 'verifycookie' => 1, 'style' => - 1, 'product' => 'mail163', 'selType' => - 1, 'secure' => 'on' );
$fields_string = http_build_query ( $fields_post, '&' );
$headers_login = array ('User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer' => 'http://www.163.com' );
//登录
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_REFERER, $refer );
curl_setopt ( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers_login );
curl_setopt ( $ch, CURLOPT_POST, count ( $fields ) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields_string );
$result = curl_exec ( $ch );
curl_close ( $ch );
//跳转
$url = 'http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=loki_wuxi';
$headers = array ('User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0' );
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
$result = curl_exec ( $ch );
curl_close ( $ch );
//取得sid
preg_match ( '/sid=[^\"].*/', $result, $location );
$sid = substr ( $location [0], 4, - 1 );
//通讯录地址
$url = 'http://g4a30.mail.163.com/jy3/address/addrlist.jsp?sid=' . $sid . '&gid=all';
$headers = array ('User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0' );
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
$result = curl_exec ( $ch );
curl_close ( $ch );
unlink ( COOKIEJAR );
//开始抓取内容
preg_match_all ( '/<td class="Ibx_Td_addrName"><a[^>]*>(.*?)<\/a><\/td><td class="Ibx_Td_addrEmail"><a[^>]*>(.*?)<\/a><\/td>/i', $result, $infos, PREG_SET_ORDER );
//1:姓名2:邮箱
print_r ( $infos );
?>
代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>EMS快递查询</title>
</head>
<body>
<?php
/* 四海网 www.q1010.com */
fclose(fopen('cookie.txt','w')); //文件 cookie.txt 用于存放取得的cookie
$cookiejar = realpath('cookie.txt');
$fp = fopen("example_homepage.txt", "w"); //文件 example_homepage.txt 用于存放取得的页面内容
$ch = curl_init("http://www.ems.com.cn/servlet/ImageCaptchaServlet");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
//readfile($cookiejar); //查看取到的 cookie
//readfile("example_homepage.jpg"); //查看取到的图片
?>
<form action="getems.php" method="post" name="form1">
快递号:<input name="mailNum" type="text" value="EA739701017CS" />(13位 首尾2位都是字母)
<input name="code" type="text" value="" />
<?php
/* 四海网 www.q1010.com */
echo "<img src='example_homepage.txt'>";?>
<input type="submit" value="提交">
</form>
</body>
</html>
代码如下:
<?php
/* 四海网 www.q1010.com */
if($_POST){
//使用先前验证码的cookie文件
$cookiejar = realpath('cookie.txt');
//获取myEmsbarCode号 和验证码变量名
$ch = curl_init("http://www.ems.com.cn");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
$result = curl_exec($ch);
curl_close($ch);
preg_match("/<input type=\"hidden\" name=\"myEmsbarCode\" value=\"(.*)\"\/>/isU",$result,$myEmsbarCode);
preg_match("/<\/span><input name=\"(.*)\" type=\"text\"/isU",$result,$codename);
$parm = array($codename[1]=>$_POST['code'],
mailNum =>$_POST['mailNum'],
myEmsbarCode=>$myEmsbarCode[1],
reqCode=>'browseBASE'
);
$ch = curl_init("http://www.ems.com.cn/qcgzOutQueryAction.do");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.ems.com.cn");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parm));
$_source = curl_exec($ch);
curl_close($ch);
//搞定
var_dump($_source);
exit;
}
?>
代码如下:
/*
*判断一个url是否为有效链接
*/
function isRealUrl($url){
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => true
);
curl_setopt_array($ch, $options);
curl_exec($ch);
if(!curl_errno($ch)){
return 200==curl_getinfo($ch,CURLINFO_HTTP_CODE)?true:false;
}
curl_close($ch);
}
$url = 'http://testpic1.tomoimg.cn/240x180/394/855/517932781/200901/12312215602409.jpg';
if(isRealUrl($url)){echo 'yes';}else{echo 'no';}
/异步请求的例子:
$userid = 517932781;
$imageid = 1520;
$albumid = 2637;
$tags = 'aa';
extract($_POST);
$url = 'http://'.$_SERVER['HTTP_HOST'].'/ajax/image.php';
$fields = array(
'userid' => $userid,
'imageid' => $imageid,
'albumid' => $albumid,
'tags' => $tags,
'optype' => 'del'
);
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields) ;
$result = curl_exec($ch) ;
curl_close($ch) ;
//上传文件
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://lh.tom.com/deal/import.php');
$fields = array(
'tname' => '道德经',
'country' => 1,
'author' => '老子',
'tags' => '道德经',
'desc' => '道可道,非常道。名可名,非常名。无名天地之始。有名万物之母。故常无欲以观其妙。常有欲以观其徼。此两者同出而异名,同谓之玄。玄之又玄,众妙之门。',
'volume' => 2,
'cover' => '@'.realpath('/data/lianhuanhua/deal/1.jpg')
);
curl_setopt($ch, CURLOPT_POST, true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);
curl_close($ch);
//多文件上传
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://lh.tom.com/deal/addpic.php');
$j = 0;
$fields = array(
'vid' => 103,
'upfile['.$j++.']' => '@'.realpath('/data/lianhuanhua/deal/1.jpg'),
'upfile['.$j++.']' => '@'.realpath('/data/lianhuanhua/deal/2.jpg')
);
curl_setopt($ch, CURLOPT_POST, true) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);
curl_close($ch);
本文来自:http://www.q1010.com/173/15619-0.html
注:关于PHP利用CURL获取邮箱地址示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:CURL,邮箱地址
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。