PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】
前置条件
要获取邮箱联系人地址,需要掌握以下知识:
- 熟悉PHP语言;
- 熟悉邮箱联系人地址的获取方式;
- 了解邮箱的认证机制;
- 了解网络请求的相关知识。
获取163邮箱联系人地址
步骤一:登录163邮箱
使用curl库,向163发起登录请求,获取登录后的cookie。代码如下:
$url = 'https://mail.163.com/';
$username = 'your_username';
$password = 'your_password';
$postdata = array('username' => $username, 'password' => $password);
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
preg_match_all('/Set-Cookie:(.*);/iU', $response, $cookies);
foreach ($cookies[1] as $cookie) {
$cookieArray = explode('=', $cookie);
if ($cookieArray[0] == 'Coremail.sid') {
$sid = $cookieArray[1];
}
}
步骤二:获取联系人地址
使用上一步获取的cookie,向163发起获取联系人地址的请求,获取联系人地址列表,代码如下:
$url = 'https://mail.163.com/contacts/call.do;jsessionid=' . $sid;
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIE => 'Coremail.sid=' . $sid,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$matches = array();
preg_match_all('/\"email\":\"(.*?)\"/iU', $response, $matches);
$emailList = $matches[1];
获取gmail邮箱联系人地址
步骤一:登录Gmail邮箱
使用curl库,向Gmail发起登录请求,获取登录后的cookie。代码如下:
$url = 'https://accounts.google.com/ServiceLoginAuth';
$username = 'your_username';
$password = 'your_password';
$postdata = array(
'Email' => $username,
'Passwd' => $password,
'continue' => 'https://www.google.com',
);
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
preg_match_all('/Set-Cookie:(.*);/iU', $response, $cookies);
$cookieStr = '';
foreach ($cookies[1] as $cookie) {
$cookieArray = explode('=', $cookie);
$name = trim($cookieArray[0]);
$value = trim($cookieArray[1]);
$cookieStr .= $name . '=' . $value . ';';
}
步骤二:获取联系人地址
使用上一步获取的cookie,向Gmail发起获取联系人地址的请求,获取联系人地址列表,代码如下:
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=10000';
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIE => $cookieStr,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$xml = simplexml_load_string($response);
$ns = $xml->getDocNamespaces();
$ns_gd = isset($ns['gd']) ? $ns['gd'] : '';
$ns_gd_prefix = $xml->getDocNamespaces(true)[$ns_gd];
$data = $xml->children($ns_gd_prefix)->entry;
$emailList = array();
foreach ($data as $contact) {
$emailList[] = (string) $contact->email[0]['address'];
}
结语
以上就是获取163、gmail邮箱联系人地址的完整攻略,需要注意的是,要获取联系人地址需要模拟登录邮箱并通过网络请求获取数据,同时需要处理对应邮件服务商的认证机制。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】 - Python技术站