Maison  >  Article  >  php教程  >  PHP飞信接收短信类

PHP飞信接收短信类

PHP中文网
PHP中文网original
2016-05-25 17:06:551366parcourir

代码

<?php
/*
*Author:Cplushua
*URI:http://weibo.com/sdnugonghua*/
Class PHPFetionRobot{
	private $tel;
	private $pwd;
	private $cookie;
	private $option = array(
		CURLOPT_URL=>&#39;&#39;,
		CURLOPT_POST=>false,
		CURLOPT_RETURNTRANSFER=>true,
		CURLOPT_REFERER=>&#39;http://f.10086.cn/im5/login/login.action&#39;,

	 );
	function __construct ($tel,$pwd){
		$this->tel=$tel;
		$this->pwd=$pwd;
	}
	function cookielogin(){
		$this->cookie=$this->readCookie();
	}
	function login($tel=null,$pwd=null){
		if(empty($tel)||empty($pwd)){
			$tel=$this->tel;
			$pwd=$this->pwd;
		}
		$option = array(
			CURLOPT_URL=> &#39;http://f.10086.cn/im5/&#39;,
			CURLOPT_REFERER=>&#39;http://f.10086.cn/wap2.jsp&#39;,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_HEADER=>true,
			CURLOPT_POST=>false,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;
		);
		$result=$this->exec($option);
		preg_match_all(&#39;/Location:\s{1}(.*)\n/&#39;, $result, $matches);
		@$url=$matches[1][0];
		if(null==$url) $url=&#39;http://f.10086.cn/im5/login/login.action&#39;;
		preg_match_all(&#39;/Set-Cookie:\s(UUID.*;)\spath.*\nSet-Cookie:\s(JSESSIONID.*;)\spath.*\n/&#39;,$result,$matches);//print_r($matches);
		$this->cookie=$matches[1][0].&#39; &#39;.$matches[2][0].&#39; path=/; HttpOnly; &#39;;
		$post_data =&#39;m=&#39;.$tel.&#39;&pass=&#39;.$pwd.&#39;&captchaCode=&checkCodeKey=null&#39;; 
		$option = array(
			CURLOPT_URL=> &#39;http://f.10086.cn/im5/login/loginHtml5.action?t=&#39;.time().&#39;780&#39;, 
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_REFERER=>$url,//&#39;http://f.10086.cn/im5/login/login.action&#39;,
			CURLOPT_POST=>true,
			CURLOPT_POSTFIELDS=>$post_data,
			CURLOPT_HEADER=>true,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;,
			CURLOPT_COOKIESESSION=>true,
			CURLOPT_COOKIE=>$this->cookie,
		);
		$result=$this->exec($option);  //echo $result;
		preg_match_all(&#39;/({.*})/&#39;, $result, $data); //print_r($data);
		preg_match_all(&#39;/Set-Cookie:\s(.*)\sHttpOnly\s\n/&#39;, $result, $matches);
		$this->cookie.=&#39;HttpOnly&#39;;
		foreach ($matches[1] as  $value) {
			$this->cookie.=&#39;; &#39;.$value;
		}
		//echo $this->cookie;
		$user_info=json_decode($data[1][0]);
		//print_r($user_info);
		$this->saveCookie($this->cookie);//echo $this->cookie;
	}
	function getonlineuser(){
		$option = array(
			CURLOPT_URL=>&#39;http://f.10086.cn/im5/index/onlineUsers.action?t=&#39;.time().&#39;017&#39;,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_REFERER =>&#39;http://f.10086.cn/im5/login/login.action&#39; , 
			CURLOPT_POST=>true,
			CURLOPT_POSTFIELDS=>&#39;gender=2&#39;,
			CURLOPT_COOKIESESSION=>true,
			CURLOPT_COOKIE=>$this->cookie,
		);
		$result=$this->exec($option);
		return  $result;
	}
	function getmsg(){echo $this->cookie;
		$option = array(
			CURLOPT_URL=>&#39;http://f.10086.cn/im5/box/alllist.action?t=&#39;.time().&#39;151&#39; ,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_REFERER=>&#39;http://f.10086.cn/im5/login/login.action?mnative=0&t=&#39;.time().&#39;561&#39;,
			CURLOPT_POST=>false,
			CURLOPT_HEADER=>false,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;,
			CURLOPT_COOKIESESSION=>true,
			CURLOPT_COOKIE=>$this->cookie,
			);
		$result=$this->exec($option);
		if(!empty($result)){
			//这里写如果有消息了,怎么处理
			$msg=json_decode($result);
            return $msg->chat_messages;
		}
	}
	function getmsgover($msgid){
		$option = array(
			CURLOPT_URL=>&#39;http://f.10086.cn/im5/chat/queryNewMsg.action?t=&#39;.time().&#39;151&_=&#39;.time().&#39;151&idMsgs=&#39;.$msgid.&#39;&t=&#39;.time().&#39;151&#39; ,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_REFERER=>&#39;http://f.10086.cn/im5/login/login.action?mnative=0&t=&#39;.time().&#39;561&#39;,
			CURLOPT_POST=>false,
			CURLOPT_HEADER=>false,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;,
			CURLOPT_COOKIESESSION=>true,
			CURLOPT_COOKIE=>$this->cookie,
		);
		$result=$this->exec($option);
		if(&#39;{"returnCode":200}&#39;==$result) return true;
		return false;
	}
	function sendSMS($aimtel,$msg){
		//需要将用户手机号码转为userid
		$userid=$this->teltouid($aimtel);
		$post_data=&#39;touserid=&#39;.$userid.&#39;&msg=&#39;.$msg;echo $post_data;
		$option = array(
			CURLOPT_URL =>&#39;http://f.10086.cn/im5/chat/sendNewMsg.action&#39; ,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_REFERER=>&#39;http://f.10086.cn/im5/login/login.action?mnative=0&t=&#39;.time().&#39;561&#39;,
			CURLOPT_POST=>true,
			CURLOPT_HEADER=>false,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;,
			CURLOPT_POSTFIELDS=>$post_data,
			CURLOPT_COOKIESESSION=>true,
			CURLOPT_COOKIE=>$this->cookie,
		);
		$result=$this->exec($option);
		if($result==&#39;{"sendCode":"true","info":"消息发送成功"}&#39;) return true;
		else return false;
	}
	function teltouid($tel){
		$option = array(
			CURLOPT_URL =>&#39;http://f.10086.cn/im5/index/searchFriendsByQueryKey.action&#39; ,
			CURLOPT_REFERER=>&#39;http://f.10086.cn/im5/login/login.action?mnative=0&t=&#39;.time().&#39;192&#39;,
			CURLOPT_POST=>true,
			CURLOPT_RETURNTRANSFER=>true,
			CURLOPT_POSTFIELDS=>&#39;queryKey=&#39;.$tel,
			CURLOPT_HEADER=>false,
			CURLOPT_USERAGENT=>&#39;Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17&#39;,
			CURLOPT_COOKIE=>$this->cookie,
			CURLOPT_COOKIESESSION=>true
		);
		$obj=json_decode($this->exec($option));echo $obj->contacts[0]->idFetion; print_r($obj);
		//echo $obj->contacts[0]->idContact;
		return $obj->contacts[0]->idContact ;
	}
	function exec($option){
		$c=curl_init();
		curl_setopt_array($c,$option);
		$result=curl_exec($c);
		curl_close($c);
		return $result;
	}
	 function sae_saveCookie($string){
		$mmc=memcache_init();
	    if($mmc==false){
	        echo "mc init failed\n"; return 0;	    	
	    }
	    else
	    {
	        return memcache_set($mmc,$this->tel,$string);
	    }
	}
	function sae_readCookie(){
		$mmc=memcache_init();
	    if($mmc==false){
	        echo "mc init failed\n"; return 0;
	    }
	    else
	    {
	        echo $res=memcache_get($mmc,$this->tel);
                return $res;
	    }

	}
	function saveCookie($string){
		if(!empty($_SERVER[&#39;HTTP_APPNAME&#39;])&&!empty($_SERVER[&#39;HTTP_APPVERSION&#39;])) return $this->sae_saveCookie($string);
		$f=fopen($this->tel.&#39;.txt&#39;, &#39;w&#39;);
		return fwrite($f, $string);
	}
	function readCookie(){
          if(isset($_SERVER[&#39;HTTP_APPNAME&#39;])&&isset($_SERVER[&#39;HTTP_APPVERSION&#39;])){  $this->cookie= $this->sae_readCookie(); return 1;}
		if(file_exists($this->tel.&#39;.txt&#39;)&&filesize($this->tel.&#39;.txt&#39;)){
			$f=fopen($this->tel.&#39;.txt&#39;, &#39;r&#39;);
			$cookie=fread($f, filesize($this->tel.&#39;.txt&#39;));
			if(!empty($cookie)) return $this->cookie=$cookie;
		}
	}
}
$f=new PHPFetionRobot(&#39;13312312311&#39;,wpwd&#39;);
$f->login(&#39;13312312311&#39;,&#39;pwd&#39;);//$f->getonlineuser();
$f->cookielogin();
$f->getonlineuser();
//$res=$f->sendSMS(&#39;1234567890&#39;,&#39;测试吧发短信&#39;);
//if($res) echo &#39;成功&#39;;
//else echo &#39;失败&#39;;
//$f->keeponline();
//$msg=$f->getmsg();
//foreach($msg as $m) print_r($m);
print_r($f->getmsg());  // 返回的是数组对象
foreach ($f->getmsg() as  $msgobj) {
	echo $f->getmsgover($msgobj->idMessage);//已经读取完毕
}
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn