第一步以下保存文件名HttpClient.class.phphost = $host; $this->port = $port; } public function __destruct() { foreach ($this as $index => $value) unset($this->$index); } public function __toString() { return $this->getContent(); } // --- Query execution methods: public function get($path, $data = null) { /* Executes a GET request for the specified path. Returns true on success and false on failure. If false, an error message describing the problem encountered can be accessed using the getError() method. $data: optional - if specified, appends it to a query string as part of the get request. $data can be an array of key value pairs, in which case a matching query string will be constructed. */ $this->path = $path; $this->method = 'GET'; if ($data) $this->path .= '?'.http_build_query($data); return $this->doRequest(); } public function post($path, $data) { /* Executes a POST request to the specified path, sending the information specified in $data. Returns true on success or false on failure. If false, an error message describing the problem encountered can be accessed using the getError() method. $data: optional - an array of key value pairs, in which case a matching post request will be constructed. */ $this->path = $path; $this->method = 'POST'; $this->postdata = http_build_query($data); $result = $this->doRequest(); $this->postdata = null; } public function ok() { // Use this after get() or post() to check the status of the last request. // Returns true if the status was 200 OK - otherwise returns false. return ($this->status == 200); } // --- Response accessors: public function getContent() { // Returns the content of the HTTP response. This is usually an HTML document. return $this->content; } public function getStatus() { // Returns the status code of the response - 200 means OK, 404 means file not found, etc. return $this->status; } public function getHeaders() { // Returns the HTTP headers returned by the server as an associative array. return $this->headers; } public function getHeader($header) { // Returns the specified response header, or false if it does not exist. $header = strtolower($header); if (isset($this->headers[$header])) { return $this->headers[$header]; } else { return false; } } public function getError() { // Returns a string describing the most recent error. return $this->errormsg; } public function getCookies($host = null) { /* Returns an array of cookies set by the server, for the current host, or (optionally) for a different host. May return null, if no cookies have been set. $host: optional - specifies a different host for which to retrieve current cookies. Defaults to using the current host. */ return @$this->cookies[$host ? $host : $this->host]; } public function getRequestURL() { // Returns the full URL that has been requested. $url = 'http://'.$this->host; if ($this->port != 80) { $url .= ':'.$this->port; } $url .= $this->path; return $url; } // --- Configuration methods: public function setUserAgent($string) { // Sets the user agent string to be used in the request. // Default is "Incutio HttpClient v$version". $this->user_agent = $string; } public function setAuthorization($username, $password) { // Sets the HTTP authorization username and password to be used in requests. // Warning: don't forget to unset this in subsequent requests to other servers! $this->username = $username; $this->password = $password; } public function setCookies($array, $replace = false) { /* Adds/overwrites or replace cookies to be sent in the request. $array: an associative array containing name-value pairs. $replace: optional, defaults to false - if true, erases all existing cookies, otherwise adds new (and overwrites existing) cookies. */ if ($replace || !is_array(@$this->cookies[$this->host])) $this->cookies[$this->host] = array(); $this->cookies[$this->host] = $this->cookies[$this->host] ; } public function useGzip($boolean) { // Specify if the client should request gzip encoded content from the server - // this saves bandwidth, but can increase processor time. Enabled by default. $this->use_gzip = $boolean; } public function setPersistCookies($boolean) { /* Specify if the client should persist cookies between requests. Enabled by default. Note: This currently ignores the cookie path (and time) completely. Time is not important, but path could possibly lead to security problems. */ $this->persist_cookies = $boolean; } public function setPersistReferers($boolean) { // Specify if the client should use the URL of the previous request as the // referral of a subsequent request. Enabled by default. $this->persist_referers = $boolean; } public function setHandleRedirects($boolean) { // Specify if the client should automatically follow redirected requests. // Enabled by default. $this->handle_redirects = $boolean; } public function setMaxRedirects($num) { // Set the maximum number of redirects allowed before the client // gives up (mainly to prevent infinite loops). Defaults to 5 redirects. $this->max_redirects = $num; } public function setHeadersOnly($boolean) { // If enabled, the client will only retrieve the headers from a page. // This could be useful for implementing things like link checkers. // Disabled by default. $this->headers_only = $boolean; } public function setDebug($boolean) { // Enables debugging messages in HTML output from the client. // Disabled by default. $this->debug = $boolean; } // --- Static utility methods: public static function quickGet($url, $data = null) { /* Static shortcut method to quickly create and configure a new instance of this class, and perform a GET query. Thanks to string-magic, you can use the return value from this method directly in an echo statement, if you like. */ $bits = parse_url($url); $host = $bits['host']; $port = isset($bits['port']) ? $bits['port'] : 80; $path = isset($bits['path']) ? $bits['path'] : '/'; if (isset($bits['query'])) $path .= '?'.$bits['query']; $client = new HttpClient($host, $port); $client->get($path, $data); return $client; } public static function quickPost($url, $data) { // Similar to [HttpClient::quickGet()], but performs a POST query. $bits = parse_url($url); $host = $bits['host']; $port = isset($bits['port']) ? $bits['port'] : 80; $path = isset($bits['path']) ? $bits['path'] : '/'; $client = new HttpClient($host, $port); $client->post($path, $data); return $client; } // --- Internal helper methods: protected function debug($msg, $object = false) { /* Displays an internal message for debugging and troubleshooting, if debugging is enabled. Use [HttpClient::setDebug()] to enable debugging. */ if ($this->debug) { echo '
' . htmlspecialchars(print_r($object,true)) . ''; echo '
第二步这里是开始绑定数据'登陆成功', '-21'=>'用户不存在', '-23'=>'密码错误', '-8'=>'需要验证', '-302'=>'开发地址验证失败',);$host = 'mp.weixin.qq.com';$user = '';//公众号$pass= '';//密码$http = new HttpClient($host);$http->setPersistCookies(true);$http->setPersistReferers(true);$http->setCookies(null);$http->get('/');$http->post('/cgi-bin/login', array('f'=>'json','imgcode'=>trim($p->imgcode),'username'=>$user,'pwd'=>md5(substr($pass, 0,16))));$content = trim($http->getContent());$jsonData = json_decode($content);header('content-type:text/html;charset=utf-8');echo '
';print_r($jsonData);//微信平台连接失败if($content==''){ }//登陆失败else if($jsonData&&$jsonData->base_resp->ret!=0){ echo $error[$jsonData->base_resp->ret];}//登陆成功else{ $redirect_url = $jsonData->redirect_url; $url_info = array(); /* Array ( [path] => /cgi-bin/home [query] => t=home/index&lang=zh_CN&token=1593764392 ) */ $url_info = parse_url($redirect_url); /* Array ( [t] => home/index [lang] => zh_CN [token] => 1593764392 ) */ parse_str($url_info['query'],$getParams); print_r($url_info); print_r($getParams); //flag=0关闭开发者模式,flag=1开启开发者模式 $http->post('/misc/skeyform?form=advancedswitchform&lang=zh_CN', array('flag'=>1,'type'=>2,'token'=>$getParams['token'])); $url_config = 'http://stealbluly.vicp.net/mpapi.html?appid=114'; $token = '62a8059ee1779a2608b6b05c26b55b6f'; //配置开发地址 $http->post('/advanced/callbackprofile?t=ajax-response&lang=zh_CN&token='.$getParams['token'], array('url'=>$url_config,'callback_token'=>$token )); $content_dev = $http->getContent(); $jsonData_dev = json_decode($content_dev); print_r($jsonData_dev); //验证成功 if($jsonData_dev->ret==0){ }//验证失败 else{ echo $error[$jsonData_dev->ret]; } //获取公众号信息 $http->get('/advanced/advanced?action=dev&t=advanced/dev&token='.$getParams['token'].'&lang=zh_CN&f=json'); $content_user_info = $http->getContent() ; $jsonData_user_info = json_decode($content_user_info); //存放微信公众号信息 $weixin_user_info = array(); //公众号信息 if($jsonData_user_info->user_info){ $weixin_user_info['user'] = array( 'nick_name'=>$jsonData_user_info->user_info->nick_name, 'user_name'=>$jsonData_user_info->user_info->user_name, ); } //公众号开发信息 if($jsonData_user_info->advanced_info){ $weixin_user_info['advanced'] = array( 'url'=>$jsonData_user_info->advanced_info->dev_info->callback_url, 'token'=>$jsonData_user_info->advanced_info->dev_info->callback_token, 'appid'=>$jsonData_user_info->advanced_info->dev_info->app_id, 'appsecret'=>$jsonData_user_info->advanced_info->dev_info->app_key, ); } print_r($weixin_user_info);}echo $content;?>