QC.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /* PHP SDK
  3. * @version 2.0.0
  4. * @author connect@qq.com
  5. * @copyright © 2013, Tencent Corporation. All rights reserved.
  6. */
  7. require_once(CLASS_PATH."Oauth.class.php");
  8. /*
  9. * @brief QC类,api外部对象,调用接口全部依赖于此对象
  10. * */
  11. class QC extends Oauth{
  12. private $kesArr, $APIMap;
  13. /**
  14. * _construct
  15. *
  16. * 构造方法
  17. * @access public
  18. * @since 5
  19. * @param string $access_token access_token value
  20. * @param string $openid openid value
  21. * @return Object QC
  22. */
  23. public function __construct($access_token = "", $openid = ""){
  24. parent::__construct();
  25. //如果access_token和openid为空,则从session里去取,适用于demo展示情形
  26. if($access_token === "" || $openid === ""){
  27. $this->keysArr = array(
  28. "oauth_consumer_key" => (int)$this->recorder->readInc("appid"),
  29. "access_token" => $this->recorder->read("access_token"),
  30. "openid" => $this->recorder->read("openid")
  31. );
  32. }else{
  33. $this->keysArr = array(
  34. "oauth_consumer_key" => (int)$this->recorder->readInc("appid"),
  35. "access_token" => $access_token,
  36. "openid" => $openid
  37. );
  38. }
  39. //初始化APIMap
  40. /*
  41. * 加#表示非必须,无则不传入url(url中不会出现该参数), "key" => "val" 表示key如果没有定义则使用默认值val
  42. * 规则 array( baseUrl, argListArr, method)
  43. */
  44. $this->APIMap = array(
  45. /* qzone */
  46. "add_blog" => array(
  47. "https://graph.qq.com/blog/add_one_blog",
  48. array("title", "format" => "json", "content" => null),
  49. "POST"
  50. ),
  51. "add_topic" => array(
  52. "https://graph.qq.com/shuoshuo/add_topic",
  53. array("richtype","richval","con","#lbs_nm","#lbs_x","#lbs_y","format" => "json", "#third_source"),
  54. "POST"
  55. ),
  56. "get_user_info" => array(
  57. "https://graph.qq.com/user/get_user_info",
  58. array("format" => "json"),
  59. "GET"
  60. ),
  61. "add_one_blog" => array(
  62. "https://graph.qq.com/blog/add_one_blog",
  63. array("title", "content", "format" => "json"),
  64. "GET"
  65. ),
  66. "add_album" => array(
  67. "https://graph.qq.com/photo/add_album",
  68. array("albumname", "#albumdesc", "#priv", "format" => "json"),
  69. "POST"
  70. ),
  71. "upload_pic" => array(
  72. "https://graph.qq.com/photo/upload_pic",
  73. array("picture", "#photodesc", "#title", "#albumid", "#mobile", "#x", "#y", "#needfeed", "#successnum", "#picnum", "format" => "json"),
  74. "POST"
  75. ),
  76. "list_album" => array(
  77. "https://graph.qq.com/photo/list_album",
  78. array("format" => "json")
  79. ),
  80. "add_share" => array(
  81. "https://graph.qq.com/share/add_share",
  82. array("title", "url", "#comment","#summary","#images","format" => "json","#type","#playurl","#nswb","site","fromurl"),
  83. "POST"
  84. ),
  85. "check_page_fans" => array(
  86. "https://graph.qq.com/user/check_page_fans",
  87. array("page_id" => "314416946","format" => "json")
  88. ),
  89. /* wblog */
  90. "add_t" => array(
  91. "https://graph.qq.com/t/add_t",
  92. array("format" => "json", "content","#clientip","#longitude","#compatibleflag"),
  93. "POST"
  94. ),
  95. "add_pic_t" => array(
  96. "https://graph.qq.com/t/add_pic_t",
  97. array("content", "pic", "format" => "json", "#clientip", "#longitude", "#latitude", "#syncflag", "#compatiblefalg"),
  98. "POST"
  99. ),
  100. "del_t" => array(
  101. "https://graph.qq.com/t/del_t",
  102. array("id", "format" => "json"),
  103. "POST"
  104. ),
  105. "get_repost_list" => array(
  106. "https://graph.qq.com/t/get_repost_list",
  107. array("flag", "rootid", "pageflag", "pagetime", "reqnum", "twitterid", "format" => "json")
  108. ),
  109. "get_info" => array(
  110. "https://graph.qq.com/user/get_info",
  111. array("format" => "json")
  112. ),
  113. "get_other_info" => array(
  114. "https://graph.qq.com/user/get_other_info",
  115. array("format" => "json", "name", "#fopenid")
  116. ),
  117. "get_fanslist" => array(
  118. "https://graph.qq.com/relation/get_fanslist",
  119. array("format" => "json", "reqnum", "startindex", "#mode", "#install", "#sex")
  120. ),
  121. "get_idollist" => array(
  122. "https://graph.qq.com/relation/get_idollist",
  123. array("format" => "json", "reqnum", "startindex", "#mode", "#install")
  124. ),
  125. "add_idol" => array(
  126. "https://graph.qq.com/relation/add_idol",
  127. array("format" => "json", "#name-1", "#fopenids-1"),
  128. "POST"
  129. ),
  130. "del_idol" => array(
  131. "https://graph.qq.com/relation/del_idol",
  132. array("format" => "json", "#name-1", "#fopenid-1"),
  133. "POST"
  134. ),
  135. /* pay */
  136. "get_tenpay_addr" => array(
  137. "https://graph.qq.com/cft_info/get_tenpay_addr",
  138. array("ver" => 1,"limit" => 5,"offset" => 0,"format" => "json")
  139. )
  140. );
  141. }
  142. //调用相应api
  143. private function _applyAPI($arr, $argsList, $baseUrl, $method){
  144. $pre = "#";
  145. $keysArr = $this->keysArr;
  146. $optionArgList = array();//一些多项选填参数必选一的情形
  147. foreach($argsList as $key => $val){
  148. $tmpKey = $key;
  149. $tmpVal = $val;
  150. if(!is_string($key)){
  151. $tmpKey = $val;
  152. if(strpos($val,$pre) === 0){
  153. $tmpVal = $pre;
  154. $tmpKey = substr($tmpKey,1);
  155. if(preg_match("/-(\d$)/", $tmpKey, $res)){
  156. $tmpKey = str_replace($res[0], "", $tmpKey);
  157. $optionArgList[$res[1]][] = $tmpKey;
  158. }
  159. }else{
  160. $tmpVal = null;
  161. }
  162. }
  163. //-----如果没有设置相应的参数
  164. if(!isset($arr[$tmpKey]) || $arr[$tmpKey] === ""){
  165. if($tmpVal == $pre){//则使用默认的值
  166. continue;
  167. }else if($tmpVal){
  168. $arr[$tmpKey] = $tmpVal;
  169. }else{
  170. if($v = $_FILES[$tmpKey]){
  171. $filename = dirname($v['tmp_name'])."/".$v['name'];
  172. move_uploaded_file($v['tmp_name'], $filename);
  173. $arr[$tmpKey] = "@$filename";
  174. }else{
  175. $this->error->showError("api调用参数错误","未传入参数$tmpKey");
  176. }
  177. }
  178. }
  179. $keysArr[$tmpKey] = $arr[$tmpKey];
  180. }
  181. //检查选填参数必填一的情形
  182. foreach($optionArgList as $val){
  183. $n = 0;
  184. foreach($val as $v){
  185. if(in_array($v, array_keys($keysArr))){
  186. $n ++;
  187. }
  188. }
  189. if(! $n){
  190. $str = implode(",",$val);
  191. $this->error->showError("api调用参数错误",$str."必填一个");
  192. }
  193. }
  194. if($method == "POST"){
  195. if($baseUrl == "https://graph.qq.com/blog/add_one_blog") $response = $this->urlUtils->post($baseUrl, $keysArr, 1);
  196. else $response = $this->urlUtils->post($baseUrl, $keysArr, 0);
  197. }else if($method == "GET"){
  198. $response = $this->urlUtils->get($baseUrl, $keysArr);
  199. }
  200. return $response;
  201. }
  202. /**
  203. * _call
  204. * 魔术方法,做api调用转发
  205. * @param string $name 调用的方法名称
  206. * @param array $arg 参数列表数组
  207. * @since 5.0
  208. * @return array 返加调用结果数组
  209. */
  210. public function __call($name,$arg){
  211. //如果APIMap不存在相应的api
  212. if(empty($this->APIMap[$name])){
  213. $this->error->showError("api调用名称错误","不存在的API: <span style='color:red;'>$name</span>");
  214. }
  215. //从APIMap获取api相应参数
  216. $baseUrl = $this->APIMap[$name][0];
  217. $argsList = $this->APIMap[$name][1];
  218. $method = isset($this->APIMap[$name][2]) ? $this->APIMap[$name][2] : "GET";
  219. if(empty($arg)){
  220. $arg[0] = null;
  221. }
  222. //对于get_tenpay_addr,特殊处理,php json_decode对\xA312此类字符支持不好
  223. if($name != "get_tenpay_addr"){
  224. $response = json_decode($this->_applyAPI($arg[0], $argsList, $baseUrl, $method));
  225. $responseArr = $this->objToArr($response);
  226. }else{
  227. $responseArr = $this->simple_json_parser($this->_applyAPI($arg[0], $argsList, $baseUrl, $method));
  228. }
  229. //检查返回ret判断api是否成功调用
  230. if($responseArr['ret'] == 0){
  231. return $responseArr;
  232. }else{
  233. return false;
  234. //$this->error->showError($response->ret, $response->msg);
  235. }
  236. }
  237. //php 对象到数组转换
  238. private function objToArr($obj){
  239. if(!is_object($obj) && !is_array($obj)) {
  240. return $obj;
  241. }
  242. $arr = array();
  243. foreach($obj as $k => $v){
  244. $arr[$k] = $this->objToArr($v);
  245. }
  246. return $arr;
  247. }
  248. /**
  249. * get_access_token
  250. * 获得access_token
  251. * @param void
  252. * @since 5.0
  253. * @return string 返加access_token
  254. */
  255. public function get_access_token(){
  256. return $this->recorder->read("access_token");
  257. }
  258. //简单实现json到php数组转换功能
  259. private function simple_json_parser($json){
  260. $json = str_replace("{","",str_replace("}","", $json));
  261. $jsonValue = explode(",", $json);
  262. $arr = array();
  263. foreach($jsonValue as $v){
  264. $jValue = explode(":", $v);
  265. $arr[str_replace('"',"", $jValue[0])] = (str_replace('"', "", $jValue[1]));
  266. }
  267. return $arr;
  268. }
  269. }