PHP获取MSN好友列表类的实现代码
<?php
error_reporting(7);
class msn
{
private $startcomm = 0;
private $username = '';
private $password = '';
private $commend = '';
private $domain = '';
private $socket = '';
private $challenge = '';
private $status = array();
private $data = array();
function set_account($username, $password)
{
$this->username = $username;
$this->password = $password;
}
function getdata(){
$buffer="";
while (!feof($this->socket)) {
$buffer .= fread($this->socket,1024);
if (preg_match("//r/",$buffer)) {
break;
}
}
$this->checkdata($buffer);
}
function getdata2() {
$buffer="";
while (!feof($this->socket)) {
$buffer .= fread($this->socket,1024);
if (preg_match("//r/n/r/n/",$buffer)) {
break;
}
}
$this->checkdata($buffer);
}
function checkdata($buffer) {
if (preg_match("/lc/=(.+?)/ui",$buffer,$matches)) {
$this->challenge = "lc=" . $matches[1];
}
if (preg_match("/(xfr 3 ns )([0-9/./:]+?) (.*) ([0-9/./:]+?)/is",$buffer,$matches)) {
$split = explode(":",$matches[2]);
$this->startcomm = 1;
$this->msn_connect($split[0],$split[1]);
}
if (preg_match("/tpf/=([a-za-z0-9]+?)/ui",$buffer,$matches)) {
$this->nexus_connect($matches[1]);
}
$split = explode("/n",$buffer);
for ($i=0;$i<count($split);$i++) {
$detail = explode(" ",$split[$i]);
if ($detail[0] == "lst") {
if(isset($detail[2])) $this->data[] = array($detail[1], urldecode($detail[2]));
}
}
$this->status = array(200, $this->data);
//echo $buffer;
}
function msn_connect($server,$port) {
if ($this->socket) {
fclose($this->socket);
}
$this->socket = @fsockopen($server,$port, $errno, $errstr, 20);
if (!$this->socket) {
$this->status = array(500,'msn验证服务器无法连接');
return false;
} else {
$this->startcomm++;
$this->send_command("ver " . $this->startcomm . " msnp8 cvr0",1);
$this->send_command("cvr " . $this->startcomm . " 0x0409 win 4.10 i386 msnmsgr 6.2 msmsgs " . $this->username,1);
$this->send_command("usr " . $this->startcomm . " twn i " . $this->username,1);
}
}
function send_command($command) {
$this->commend = $command;
$this->startcomm++;
fwrite($this->socket,$command . "/r/n");
$this->getdata();
}
function nexus_connect($tpf) {
$arr[] = "get /rdr/pprdr.asp http/1.0/r/n/r/n";
$curl = curl_init();
curl_setopt($curl, curlopt_url, "https://nexus.passport.com:443/rdr/pprdr.asp");
curl_setopt($curl, curlopt_returntransfer, 1);
curl_setopt($curl, curlopt_verbose, 0);
curl_setopt($curl, curlopt_header,1);
curl_setopt($curl, curlopt_httpheader, $arr);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
$data = curl_exec($curl);
curl_close($curl);
preg_match("/dalogin=(.+?),/",$data,$matches);
if(!isset($matches[1])) return false;
$split = explode("/",$matches[1]);
$headers[0] = "get /$split[1] http/1.1/r/n";
$headers[1] = "authorization: passport1.4 orgverb=get,orgurl=http%3a%2f%2fmessenger%2emsn%2ecom,sign-in=" . $this->username . ",pwd=" . $this->password . ", " . trim($this->challenge) . "/r/n";
$curl = curl_init();
curl_setopt($curl, curlopt_url, "https://" . $split[0] . ":443/". $split[1]);
curl_setopt($curl, curlopt_returntransfer, 1);
curl_setopt($curl, curlopt_verbose, 0);
curl_setopt($curl,curlopt_followlocation,1);
curl_setopt($curl, curlopt_httpheader, $headers);
curl_setopt($curl, curlopt_header,1);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
$data = curl_exec($curl);
curl_close($curl);
preg_match("/t=(.+?)'/",$data,$matches);
if(!isset($matches[1])){
$this->status = array(404, '你输入的msn帐号或者密码错误');
return false;
}
$this->send_command("usr " . $this->startcomm . " twn s t=" . trim($matches[1]) . "",2);
$this->send_command("chg " . $this->startcomm . " hdn",2);
$this->send_command("syn " . $this->startcomm . " 0",2);
$this->getdata2();
$this->send_command("syn " . $this->startcomm . " 1 46 2",2);
$this->getdata2();
$this->send_command("chg ". $this->startcomm . " bsy");
$this->getdata();
}
public function getstatus()
{
return $this->status;
}
}
$msn = new msn;
$msn->set_account('xx@hotmail.com', 'xxxxx');
$msn->msn_connect("messenger.hotmail.com",1863);
$data = $msn->getstatus();
print_r($data);
?>