Browse Source

fixed login problem in wechatext for Issue #17

dodgepudding 11 years ago
parent
commit
0f719b74e0
6 changed files with 35 additions and 36 deletions
  1. 3 3
      Thinkphp/Snoopy.class.php
  2. 10 14
      Thinkphp/Wechatext.class.php
  3. 3 3
      snoopy.class.php
  4. 1 1
      test/test1.php
  5. 2 0
      test/test2.php
  6. 16 15
      wechatext.class.php

+ 3 - 3
Thinkphp/Snoopy.class.php

@@ -63,7 +63,7 @@ class Snoopy
 	var	$pass			=	"";					// password for http authentication
 
 	// http accept types
-	var $accept			=	"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
+	var $accept			=	"application/json, text/javascript, */*; q=0.01";
 
 	var $results		=	"";					// where the content is put
 
@@ -791,7 +791,7 @@ class Snoopy
 			$headers .= "User-Agent: ".$this->agent."\r\n";
 		if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
 			$headers .= "Host: ".$this->host;
-			if(!empty($this->port))
+			if(!empty($this->port) && $this->port!=80)
 				$headers .= ":".$this->port;
 			$headers .= "\r\n";
 		}
@@ -957,7 +957,7 @@ class Snoopy
 		if(!empty($this->agent))
 			$headers[] = "User-Agent: ".$this->agent;
 		if(!empty($this->host))
-			if(!empty($this->port))
+			if(!empty($this->port) && $this->port!=80)
 			$headers[] = "Host: ".$this->host.":".$this->port;
 		else
 			$headers[] = "Host: ".$this->host;

+ 10 - 14
Thinkphp/Wechatext.class.php

@@ -393,27 +393,23 @@ class Wechatext
 		$post["username"] = $this->_account;
 		$post["pwd"] = md5($this->_password);
 		$post["f"] = "json";
+		$post["imgcode"] = "";
+		$snoopy->referer = "https://mp.weixin.qq.com/";
 		$snoopy->submit($submit,$post);
 		$cookie = '';
-		$this->log($snoopy->headers);
+		$this->log($snoopy->results);
+		$result = json_decode($snoopy->results,true);
+		if ($result['ErrCode']!=0) return false;
 		foreach ($snoopy->headers as $key => $value) {
 			$value = trim($value);
 			if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
 				$cookie .=$match[1].'='.$match[2].'; ';
 		}
-		if ($cookie) {
-			$send_snoopy = new Snoopy; 
-			$send_snoopy->rawheaders['Cookie']= $cookie;
-			$send_snoopy->maxredirs = 0;
-			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
-			$send_snoopy->fetch($url);
-			$header = implode(',',$send_snoopy->headers);
-			$this->log('header:'.print_r($send_snoopy->headers,true));
-			preg_match("/token=(\d+)/i",$header,$matches);
-			if($matches){
-				$this->_token = $matches[1];
-				$this->log('token:'.$this->_token);
-			}
+		
+		preg_match("/token=(\d+)/i",$result['ErrMsg'],$matches);
+		if($matches){
+			$this->_token = $matches[1];
+			$this->log('token:'.$this->_token);
 		}
 		$this->saveCookie($this->_cookiename,$cookie);
 		return $cookie;

+ 3 - 3
snoopy.class.php

@@ -63,7 +63,7 @@ class Snoopy
 	var	$pass			=	"";					// password for http authentication
 
 	// http accept types
-	var $accept			=	"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
+	var $accept			=	"application/json, text/javascript, */*; q=0.01";
 
 	var $results		=	"";					// where the content is put
 
@@ -791,7 +791,7 @@ class Snoopy
 			$headers .= "User-Agent: ".$this->agent."\r\n";
 		if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
 			$headers .= "Host: ".$this->host;
-			if(!empty($this->port))
+			if(!empty($this->port) && $this->port!=80)
 				$headers .= ":".$this->port;
 			$headers .= "\r\n";
 		}
@@ -957,7 +957,7 @@ class Snoopy
 		if(!empty($this->agent))
 			$headers[] = "User-Agent: ".$this->agent;
 		if(!empty($this->host))
-			if(!empty($this->port))
+			if(!empty($this->port) && $this->port!=80)
 			$headers[] = "Host: ".$this->host.":".$this->port;
 		else
 			$headers[] = "Host: ".$this->host;

+ 1 - 1
test/test1.php

@@ -10,7 +10,7 @@
 	};
 	$options = array(
 		'token'=>'tokenaccesskey', //填写你设定的key
-			'debug'=>true,
+			'debug'=>true,
 			'logcallback'=>'logdebug'
 	);
 	$weObj = new Wechat($options);

+ 2 - 0
test/test2.php

@@ -26,4 +26,6 @@
 		// 主动回复消息
 		if ($topmsg && $topmsg['hasReply']==0)
 		$wechat->send($topmsg['fakeId'],'hi '.$topmsg['nickName'].',rev:'.$topmsg['content']);	
+	} else {
+		echo "login error";
 	}

+ 16 - 15
wechatext.class.php

@@ -388,33 +388,33 @@ class Wechatext
 	 * 模拟登录获取cookie
 	 * @return [type] [description]
 	 */
+	/**
+	 * 模拟登录获取cookie
+	 * @return [type] [description]
+	 */
 	public function login(){
 		$snoopy = new Snoopy; 
-		$submit = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
+		$submit = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
 		$post["username"] = $this->_account;
 		$post["pwd"] = md5($this->_password);
 		$post["f"] = "json";
+		$post["imgcode"] = "";
+		$snoopy->referer = "https://mp.weixin.qq.com/";
 		$snoopy->submit($submit,$post);
 		$cookie = '';
-		$this->log($snoopy->headers);
+		$this->log($snoopy->results);
+		$result = json_decode($snoopy->results,true);
+		if ($result['ErrCode']!=0) return false;
 		foreach ($snoopy->headers as $key => $value) {
 			$value = trim($value);
 			if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $value,$match))
 				$cookie .=$match[1].'='.$match[2].'; ';
 		}
-		if ($cookie) {
-			$send_snoopy = new Snoopy; 
-			$send_snoopy->rawheaders['Cookie']= $cookie;
-			$send_snoopy->maxredirs = 0;
-			$url = "https://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
-			$send_snoopy->fetch($url);
-			$header = implode(',',$send_snoopy->headers);
-			$this->log('header:'.print_r($send_snoopy->headers,true));
-			preg_match("/token=(\d+)/i",$header,$matches);
-			if($matches){
-				$this->_token = $matches[1];
-				$this->log('token:'.$this->_token);
-			}
+		
+		preg_match("/token=(\d+)/i",$result['ErrMsg'],$matches);
+		if($matches){
+			$this->_token = $matches[1];
+			$this->log('token:'.$this->_token);
 		}
 		$this->saveCookie($this->_cookiename,$cookie);
 		return $cookie;
@@ -471,6 +471,7 @@ class Wechatext
 	 */
 	public function checkValid()
 	{
+		if (!$this->cookie || !$this->_token) return false;
 		$send_snoopy = new Snoopy; 
 		$post = array('ajax'=>1,'token'=>$this->_token);
 		$submit = "https://mp.weixin.qq.com/cgi-bin/getregions?id=1017&t=ajax-getregions&lang=zh_CN";