Heim  >  Artikel  >  php教程  >  腾讯微博的账号登录及api操作

腾讯微博的账号登录及api操作

PHP中文网
PHP中文网Original
2016-05-25 17:06:591472Durchsuche

腾讯微博 的账号登录及api操作,使用oauth 2.0 官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布微博等功能,如果需要其他功能可以根据官方的api文档自行添加

tqq.php

<?php
/**
 * PHP Library for t.qq.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class tqqPHP
{
	function __construct($client_id, $client_secret, $access_token=NULL, $openid=NULL){
		$this->client_id=$client_id;
		$this->client_secret=$client_secret;
		$this->access_token=$access_token;
		$this->openid=$openid;
	}

	function login_url($callback_url){
		$params=array(
			&#39;response_type&#39;=>&#39;code&#39;,
			&#39;client_id&#39;=>$this->client_id,
			&#39;redirect_uri&#39;=>$callback_url
		);
		return &#39;https://open.t.qq.com/cgi-bin/oauth2/authorize?&#39;.http_build_query($params);
	}

	function access_token($callback_url, $code){
		$params=array(
			&#39;grant_type&#39;=>&#39;authorization_code&#39;,
			&#39;code&#39;=>$code,
			&#39;client_id&#39;=>$this->client_id,
			&#39;client_secret&#39;=>$this->client_secret,
			&#39;redirect_uri&#39;=>$callback_url
		);
		$url=&#39;https://open.t.qq.com/cgi-bin/oauth2/access_token?&#39;.http_build_query($params);
		$result_str=$this->http($url);
		$json_r=array();
		if($result_str!=&#39;&#39;)parse_str($result_str, $json_r);
		return $json_r;
	}

	function access_token_refresh($refresh_token){
		$params=array(
			&#39;grant_type&#39;=>&#39;refresh_token&#39;,
			&#39;refresh_token&#39;=>$refresh_token,
			&#39;client_id&#39;=>$this->client_id
		);
		$url=&#39;https://open.t.qq.com/cgi-bin/oauth2/access_token?&#39;.http_build_query($params);
		$result_str=$this->http($url);
		$json_r=array();
		if($result_str!=&#39;&#39;)parse_str($result_str, $json_r);
		return $json_r;
	}

	function me(){
		$params=array();
		$url=&#39;https://open.t.qq.com/api/user/info&#39;;
		return $this->api($url, $params);
	}

	function getMyTweet($reqnum=10, $pageflag=0){
		$params=array(
			&#39;pageflag&#39;=>$pageflag,
			&#39;reqnum&#39;=>$reqnum
		);
		$url=&#39;https://open.t.qq.com/api/statuses/broadcast_timeline&#39;;
		return $this->api($url, $params);
	}

	function getRecount($ids){
		$params=array(
			&#39;ids&#39;=>$ids,
			&#39;flag&#39;=>2
		);
		$url=&#39;https://open.t.qq.com/api/t/re_count&#39;;
		return $this->api($url, $params);
	}

	function getReplay($id, $flag=0, $f=0, $n=10){
		$params=array(
			&#39;rootid&#39;=>$id,
			&#39;pageflag&#39;=>$f,
			&#39;reqnum&#39;=>$n,
			&#39;flag&#39;=>$flag
		);
		$url=&#39;https://open.t.qq.com/api/t/re_list&#39;;
		return $this->api($url, $params);
	}

	function postOne($img_c, $pic=&#39;&#39;){
		$params=array(
			&#39;content&#39;=>$img_c
		);
		if($pic!=&#39;&#39; && is_array($pic)){
			$url=&#39;https://open.t.qq.com/api/t/add_pic&#39;;
			$params[&#39;pic&#39;]=$pic;
		}else{
			$url=&#39;https://open.t.qq.com/api/t/add&#39;;
		}
		return $this->api($url, $params, &#39;POST&#39;);
	}

	function api($url, $params, $method=&#39;GET&#39;){
		$params[&#39;oauth_consumer_key&#39;]=$this->client_id;
		$params[&#39;access_token&#39;]=$this->access_token;
		$params[&#39;openid&#39;]=$this->openid;
		$params[&#39;clientip&#39;]=$this->getIP();
		$params[&#39;oauth_version&#39;]=&#39;2.a&#39;;
		$params[&#39;format&#39;]=&#39;json&#39;;
		$params[&#39;scope&#39;]=&#39;all&#39;;
		if($method==&#39;GET&#39;){
			$result_str=$this->http($url.&#39;?&#39;.http_build_query($params));
		}else{
			if(isset($params[&#39;pic&#39;])){
				uksort($params, &#39;strcmp&#39;);
				$str_b=uniqid(&#39;------------------&#39;);
				$str_m=&#39;--&#39;.$str_b;
				$str_e=$str_m. &#39;--&#39;;
				$body=&#39;&#39;;
				foreach($params as $k=>$v){
					if($k==&#39;pic&#39;){
						if(is_array($v)){
							$img_c=$v[2];
							$img_n=$v[1];
						}elseif($v{0}==&#39;@&#39;){
							$url=ltrim($v, &#39;@&#39;);
							$img_c=file_get_contents($url);
							$url_a=explode(&#39;?&#39;, basename($url));
							$img_n=$url_a[0];
						}
						$body.=$str_m."\r\n";
						$body.=&#39;Content-Disposition: form-data; name="&#39;.$k.&#39;"; filename="&#39;.$img_n.&#39;"&#39;."\r\n";
						$body.="Content-Type: image/unknown\r\n\r\n";
						$body.=$img_c."\r\n";
					}else{
						$body.=$str_m."\r\n";
						$body.=&#39;Content-Disposition: form-data; name="&#39;.$k."\"\r\n\r\n";
						$body.=$v."\r\n";
					}
				}
				$body.=$str_e;
				$headers[]="Content-Type: multipart/form-data; boundary=".$str_b;
				$result_str=$this->http($url, $body, &#39;POST&#39;, $headers);
			}else{
				$result_str=$this->http($url, http_build_query($params), &#39;POST&#39;);
			}
		}
		$json_r=array();
		if($result_str!=&#39;&#39;)$json_r=json_decode($result_str, true);
		return $json_r;
	}

	function getIP(){
		if(isset($_ENV[&#39;HTTP_CLIENT_IP&#39;])){
			$ip=$_ENV[&#39;HTTP_CLIENT_IP&#39;];
		}elseif(isset($_ENV[&#39;HTTP_X_FORWARDED_FOR&#39;])){
			$ip=$_ENV[&#39;HTTP_X_FORWARDED_FOR&#39;];
		}elseif(isset($_ENV[&#39;REMOTE_ADDR&#39;])){
			$ip=$_ENV[&#39;REMOTE_ADDR&#39;];
		}else{
			$ip=$_SERVER[&#39;REMOTE_ADDR&#39;];
		}
		if(strstr($ip, &#39;:&#39;)){
			$ipa=explode(&#39;:&#39;, $ip);
			foreach($ipa as $v){
				if(strlen($v)>7)$ip=$v;
			}
		}
		if(strlen($ip)<7)$ip=&#39;0.0.0.0&#39;;
		return $ip;
	}

	function http($url, $postfields=&#39;&#39;, $method=&#39;GET&#39;, $headers=array()){
		$ci=curl_init();
		curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ci, CURLOPT_TIMEOUT, 30);
		if($method==&#39;POST&#39;){
			curl_setopt($ci, CURLOPT_POST, TRUE);
			if($postfields!=&#39;&#39;)curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
		}
		$headers[]="User-Agent: tqqPHP(piscdong.com)";
		curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ci, CURLOPT_URL, $url);
		$response=curl_exec($ci);
		curl_close($ci);
		return $response;
	}
}
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:QQ的账号登录及api操作Nächster Artikel:删除目录及目录下文件