存档

文章标签 ‘CURL’

基于WebQQ3.0协议写一个QQ机器人

2013年4月21日 12 条评论

一个偶然的机会,在某群看到一个类似QQ机器人的玩意在骚动,问那哥们回答说是JAVA写的,原来QQ还能这样玩儿。于是乎,我也就开始骚动了。Let’s go

1.WEBQQ3.0登陆协议

进入WEBQQ, http://web.qq.com/
通过工具分析,可以知道,用户在输入密码之前(也就是输入[......]

Read more

分类: PHP 标签: , ,

PHP CURL模拟登录163邮箱抓取好友列表

2013年3月9日 1 条评论
<?php
$username	="username@163.com";
$password	="password";
$robot = new web_robot();
// 获取163联系人
$address_list = $robot->get_address_list($username, $password);
print_r($address_list);
 
/**
 * 模拟登录163邮箱抓取好友列表
 * @copyright (c) 2013
 * @author qiufeng <fengdingbo@gmail.com>
 * @link	http://www.fengdingbo.com
 * @version 1.0
 */
class web_robot{
 
	/**
	 * 存放cookie信息的临时文件名 
	 * @var string
	 */
	public $cookie_tmp_file;
 
	public function __construct()
	{
		$this->cookie_tmp_file = tempnam( ini_get( "upload_tmp_dir" ), "cookie" );
	}
 
	public function __destruct()
	{
		// 删除临时COOKIE
		unlink($this->cookie_tmp_file);
	}
 
	/**
	 * 获取联系人列表
	 * || ============================
	 * || 返回值说明:
	 * || ----------------------------
	 * || int data type:
	 * || 201:用户名或密码不正确.
	 * || 202:服务器异常,请稍后再试.
	 * || 203:没有找到邮箱联系人.
	 * || 
	 * || array data type:
	 * || 获取联系人列表成功.
	 * || ============================
	 * @param string $username
	 * @param string $password
	 * @return mixed
	 */
	public function get_address_list($username, $password)
	{
		if ( ! $this->login($username, $password))
		{
			return 201;
		}
 
		$header = $this->_getheader($username);
 
		if ( ! $header['sid'])
		{
			return 202;
		}
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://" . $header['host'] . "/js4/s?sid=" . $header['sid'] . "&func=global:sequential");
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_tmp_file);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language: zh-cn', 'Connection: Keep-Alive', 'Content-Type: application/xml; charset=UTF-8'));
		$str = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string>" .
				"<object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object>" .
				"</array></object></object><object><string name=\"func\">user:getSignatures</string></object>" .
				"<object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
 
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		ob_start();
		curl_exec($ch);
		$data = ob_get_clean();
 
		// 把字符转转化为SimpleXMLElement Object
		$XML = (array)simplexml_load_string($data)->array->object[0]->array;
		if (array_key_exists('object', $XML))
		{
			foreach($XML['object'] as $v)
			{
				$array_format = (array)$v;
				$list[] = $array_format['string'];
			}
 
			return $list;
		}
 
		return 203; }
 
	/**
	 * 登陆163邮箱
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	private function login($username, $password)
	{
		// 登陆163,保存cookie
		$cookies = array();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
		curl_setopt($ch, CURLOPT_URL, "http://reg.163.com/logins.jsp");
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password."&type=1");
 
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_tmp_file);
		curl_setopt($ch,CURLOPT_HEADER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //文件流形式返回
		$str = curl_exec($ch);
		curl_close($ch);
 
		preg_match('/(window.location.replace\(\"(.*)\"\))/i', $str,$content);
 
		if (isset($content) && is_array($content) && array_key_exists(2, $content))
		{
			ob_start();
			$ch = curl_init($content[2]);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			curl_setopt($ch,CURLOPT_COOKIEFILE, $this->cookie_tmp_file);
			$str2 = curl_exec($ch);
			curl_close($ch);
			$content = ob_get_clean();
 
			// 校验是否登陆成功
			if (strpos($content, "安全退出") && strpos($content, $username))
			{
				return TRUE;
			}
		}
 
		return FALSE;
	}
 
	/**
	 * 获取头部信息
	 * @param string $username
	 * @return array
	 */
	private function _getheader($username)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=" . $username);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_tmp_file);  //当前使用的cookie
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_tmp_file);   //服务器返回的新cookie
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_NOBODY, true);
		$content = curl_exec($ch);
		curl_close($ch);
 
		preg_match_all('/Location:\s*(.*?)\r\n/i', $content, $regs);
		$refer = $regs[1][0];
		preg_match_all('/http\:\/\/(.*?)\//i', $refer, $regs);
		$host = $regs[1][0];
		preg_match_all("/sid=(.*)/i", $refer, $regs);
		$sid = $regs[1][0];
 
		return array('sid' => $sid, 'refer' => $refer, 'host' => $host);
	}
}
?>
分类: PHP 标签: ,