qqclient.php 9.8 KB

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