php使用腾讯QQ微博API接口获取微博内容的代码

WBOY
Libérer: 2016-07-25 08:57:47
original
1212 Les gens l'ont consulté
  1. /**

  2. * 使用腾讯QQ微博API接口获取微博内容
  3. * by bbs.it-home.org
  4. */
  5. define('HDOM_TYPE_ELEMENT', 1);
  6. define('HDOM_TYPE_COMMENT', 2);
  7. define('HDOM_TYPE_TEXT', 3);
  8. define('HDOM_TYPE_ENDTAG', 4);
  9. define('HDOM_TYPE_ROOT', 5);
  10. define('HDOM_TYPE_UNKNOWN', 6);
  11. define('HDOM_QUOTE_DOUBLE', 0);
  12. define('HDOM_QUOTE_SINGLE', 1);
  13. define('HDOM_QUOTE_NO', 3);
  14. define('HDOM_INFO_BEGIN', 0);
  15. define('HDOM_INFO_END', 1);
  16. define('HDOM_INFO_QUOTE', 2);
  17. define('HDOM_INFO_SPACE', 3);
  18. define('HDOM_INFO_TEXT', 4);
  19. define('HDOM_INFO_INNER', 5);
  20. define('HDOM_INFO_OUTER', 6);
  21. define('HDOM_INFO_ENDSPACE',7);
  22. // helper functions
  23. // ---------------
  24. // get html dom form file
  25. function file_get_html() {
  26. $dom = new simple_html_dom;
  27. $args = func_get_args();
  28. $dom->load(call_user_func_array('file_get_contents', $args), true);
  29. return $dom;
  30. }
  31. // get html dom form string
  32. function str_get_html($str, $lowercase=true) {
  33. $dom = new simple_html_dom;
  34. $dom->load($str, $lowercase);
  35. return $dom;
  36. }
  37. // dump html dom tree
  38. function dump_html_tree($node, $show_attr=true, $deep=0) {
  39. $lead = str_repeat(' ', $deep);
  40. echo $lead.$node->tag;
  41. if ($show_attr && count($node->attr)>0) {
  42. echo '(';
  43. foreach($node->attr as $k=>$v)
  44. echo "[$k]=>\"".$node->$k.'", ';
  45. echo ')';
  46. }
  47. echo "\n";
  48. foreach($node->nodes as $c)
  49. dump_html_tree($c, $show_attr, $deep+1);
  50. }
  51. // get dom form file (dePRecated)
  52. function file_get_dom() {
  53. $dom = new simple_html_dom;
  54. $args = func_get_args();
  55. $dom->load(call_user_func_array('file_get_contents', $args), true);
  56. return $dom;
  57. }
  58. // get dom form string (deprecated)
  59. function str_get_dom($str, $lowercase=true) {
  60. $dom = new simple_html_dom;
  61. $dom->load($str, $lowercase);
  62. return $dom;
  63. }
  64. // simple html dom node
  65. // ---------------
  66. class simple_html_dom_node {
  67. public $nodetype = HDOM_TYPE_TEXT;
  68. public $tag = 'text';
  69. public $attr = array();
  70. public $children = array();
  71. public $nodes = array();
  72. public $parent = null;
  73. public $_ = array();
  74. private $dom = null;
  75. function __construct($dom) {
  76. $this->dom = $dom;
  77. $dom->nodes[] = $this;
  78. }
  79. function __destruct() {
  80. $this->clear();
  81. }
  82. function __toString() {
  83. return $this->outertext();
  84. }
  85. // clean up memory due to php5 circular references memory leak...
  86. function clear() {
  87. $this->dom = null;
  88. $this->nodes = null;
  89. $this->parent = null;
  90. $this->children = null;
  91. }
  92. // dump node's tree

  93. function dump($show_attr=true) {
  94. dump_html_tree($this, $show_attr);
  95. }
  96. // returns the parent of node
  97. function parent() {
  98. return $this->parent;
  99. }
  100. // returns children of node
  101. function children($idx=-1) {
  102. if ($idx===-1) return $this->children;
  103. if (isset($this->children[$idx])) return $this->children[$idx];
  104. return null;
  105. }
  106. // returns the first child of node
  107. function first_child() {
  108. if (count($this->children)>0) return $this->children[0];
  109. return null;
  110. }
  111. // returns the last child of node
  112. function last_child() {
  113. if (($count=count($this->children))>0) return $this->children[$count-1];
  114. return null;
  115. }
  116. // returns the next sibling of node
  117. function next_sibling() {
  118. if ($this->parent===null) return null;
  119. $idx = 0;
  120. $count = count($this->parent->children);
  121. while ($idxparent->children[$idx])
  122. ++$idx;
  123. if (++$idx>=$count) return null;
  124. return $this->parent->children[$idx];
  125. }
  126. // returns the previous sibling of node
  127. function prev_sibling() {
  128. if ($this->parent===null) return null;
  129. $idx = 0;
  130. $count = count($this->parent->children);
  131. while ($idxparent->children[$idx])
  132. ++$idx;
  133. if (--$idx return $this->parent->children[$idx];
  134. }
  135. // get dom node's inner html
  136. function innertext() {
  137. if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
  138. if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
  139. $ret = '';
  140. foreach($this->nodes as $n)
  141. $ret .= $n->outertext();
  142. return $ret;
  143. }
  144. // get dom node's outer text (with tag)
  145. function outertext() {
  146. if ($this->tag==='root') return $this->innertext();
  147. // trigger callback
  148. if ($this->dom->callback!==null)
  149. call_user_func_array($this->dom->callback, array($this));
  150. if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER];
  151. if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
  152. // render begin tag
  153. $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();
  154. // render inner text
  155. if (isset($this->_[HDOM_INFO_INNER]))
  156. $ret .= $this->_[HDOM_INFO_INNER];
  157. else {
  158. foreach($this->nodes as $n)
  159. $ret .= $n->outertext();
  160. }
  161. // render end tag
  162. if(isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0)
  163. $ret .= ''.$this->tag.'>';
  164. return $ret;
  165. }
  166. // get dom node's plain text
  167. function text() {
  168. if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
  169. switch ($this->nodetype) {
  170. case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
  171. case HDOM_TYPE_COMMENT: return '';
  172. case HDOM_TYPE_UNKNOWN: return '';
  173. }
  174. if (strcasecmp($this->tag, 'script')===0) return '';
  175. if (strcasecmp($this->tag, 'style')===0) return '';
  176. $ret = '';
  177. foreach($this->nodes as $n)
  178. $ret .= $n->text();
  179. return $ret;
  180. }
  181. function xmltext() {

  182. $ret = $this->innertext();
  183. $ret = str_ireplace(' $ret = str_replace(']]>', '', $ret);
  184. return $ret;
  185. }
  186. // build node's text with tag
  187. function makeup() {
  188. // text, comment, unknown
  189. if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
  190. $ret = 'tag;
  191. $i = -1;
  192. foreach($this->attr as $key=>$val) {
  193. ++$i;
  194. // skip removed attribute
  195. if ($val===null || $val===false)
  196. continue;
  197. $ret .= $this->_[HDOM_INFO_SPACE][$i][0];
  198. //no value attr: nowrap, checked selected...
  199. if ($val===true)
  200. $ret .= $key;
  201. else {
  202. switch($this->_[HDOM_INFO_QUOTE][$i]) {
  203. case HDOM_QUOTE_DOUBLE: $quote = '"'; break;
  204. case HDOM_QUOTE_SINGLE: $quote = '\''; break;
  205. default: $quote = '';
  206. }
  207. $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;
  208. }
  209. }
  210. $ret = $this->dom->restore_noise($ret);
  211. return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>';
  212. }
  213. // find elements by CSS selector
  214. function find($selector, $idx=null) {
  215. $selectors = $this->parse_selector($selector);
  216. if (($count=count($selectors))===0) return array();
  217. $found_keys = array();
  218. // find each selector
  219. for ($c=0; $c if (($levle=count($selectors[0]))===0) return array();
  220. if (!isset($this->_[HDOM_INFO_BEGIN])) return array();
  221. $head = array($this->_[HDOM_INFO_BEGIN]=>1);
  222. // handle descendant selectors, no recursive!
  223. for ($l=0; $l $ret = array();
  224. foreach($head as $k=>$v) {
  225. $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k];
  226. $n->seek($selectors[$c][$l], $ret);
  227. }
  228. $head = $ret;
  229. }
  230. foreach($head as $k=>$v) {
  231. if (!isset($found_keys[$k]))
  232. $found_keys[$k] = 1;
  233. }
  234. }
  235. // sort keys
  236. ksort($found_keys);
  237. $found = array();
  238. foreach($found_keys as $k=>$v)
  239. $found[] = $this->dom->nodes[$k];
  240. // return nth-element or array
  241. if (is_null($idx)) return $found;
  242. else if ($idx return (isset($found[$idx])) ? $found[$idx] : null;
  243. }
  244. // seek for given conditions
  245. protected function seek($selector, &$ret) {
  246. list($tag, $key, $val, $exp, $no_key) = $selector;
  247. // xpath index
  248. if ($tag && $key && is_numeric($key)) {
  249. $count = 0;
  250. foreach ($this->children as $c) {
  251. if ($tag==='*' || $tag===$c->tag) {
  252. if (++$count==$key) {
  253. $ret[$c->_[HDOM_INFO_BEGIN]] = 1;
  254. return;
  255. }
  256. }
  257. }
  258. return;
  259. }
  260. $end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0;
  261. if ($end==0) {
  262. $parent = $this->parent;
  263. while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) {
  264. $end -= 1;
  265. $parent = $parent->parent;
  266. }
  267. $end += $parent->_[HDOM_INFO_END];
  268. }
  269. for($i=$this->_[HDOM_INFO_BEGIN]+1; $i $node = $this->dom->nodes[$i];
  270. $pass = true;
  271. if ($tag==='*' && !$key) {
  272. if (in_array($node, $this->children, true))
  273. $ret[$i] = 1;
  274. continue;
  275. }
  276. // compare tag
  277. if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;}
  278. // compare key
  279. if ($pass && $key) {
  280. if ($no_key) {
  281. if (isset($node->attr[$key])) $pass=false;
  282. }
  283. else if (!isset($node->attr[$key])) $pass=false;
  284. }
  285. // compare value
  286. if ($pass && $key && $val && $val!=='*') {
  287. $check = $this->match($exp, $val, $node->attr[$key]);
  288. // handle multiple class
  289. if (!$check && strcasecmp($key, 'class')===0) {
  290. foreach(explode(' ',$node->attr[$key]) as $k) {
  291. $check = $this->match($exp, $val, $k);
  292. if ($check) break;
  293. }
  294. }
  295. if (!$check) $pass = false;
  296. }
  297. if ($pass) $ret[$i] = 1;
  298. unset($node);
  299. }
  300. }
  301. protected function match($exp, $pattern, $value) {
  302. switch ($exp) {
  303. case '=':
  304. return ($value===$pattern);
  305. case '!=':
  306. return ($value!==$pattern);
  307. case '^=':
  308. return preg_match("/^".preg_quote($pattern,'/')."/", $value);
  309. case '$=':
  310. return preg_match("/".preg_quote($pattern,'/')."$/", $value);
  311. case '*=':
  312. if ($pattern[0]=='/')
  313. return preg_match($pattern, $value);
  314. return preg_match("/".$pattern."/i", $value);
  315. }
  316. return false;
  317. }
  318. protected function parse_selector($selector_string) {
  319. // pattern of CSS selectors, modified from mootools
  320. $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";
  321. preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER);
  322. $selectors = array();
  323. $result = array();
  324. //print_r($matches);
  325. foreach ($matches as $m) {
  326. $m[0] = trim($m[0]);
  327. if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue;
  328. // for borwser grnreated xpath
  329. if ($m[1]==='tbody') continue;
  330. list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false);
  331. if(!empty($m[2])) {$key='id'; $val=$m[2];}
  332. if(!empty($m[3])) {$key='class'; $val=$m[3];}
  333. if(!empty($m[4])) {$key=$m[4];}
  334. if(!empty($m[5])) {$exp=$m[5];}
  335. if(!empty($m[6])) {$val=$m[6];}
  336. // convert to lowercase
  337. if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);}
  338. //elements that do NOT have the specified attribute
  339. if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;}
  340. $result[] = array($tag, $key, $val, $exp, $no_key);
  341. if (trim($m[7])===',') {
  342. $selectors[] = $result;
  343. $result = array();
  344. }
  345. }
  346. if (count($result)>0)
  347. $selectors[] = $result;
  348. return $selectors;
  349. }
  350. function __get($name) {
  351. if (isset($this->attr[$name])) return $this->attr[$name];
  352. switch($name) {
  353. case 'outertext': return $this->outertext();
  354. case 'innertext': return $this->innertext();
  355. case 'plaintext': return $this->text();
  356. case 'xmltext': return $this->xmltext();
  357. default: return array_key_exists($name, $this->attr);
  358. }
  359. }
  360. function __set($name, $value) {
  361. switch($name) {
  362. case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value;
  363. case 'innertext':
  364. if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value;
  365. return $this->_[HDOM_INFO_INNER] = $value;
  366. }
  367. if (!isset($this->attr[$name])) {
  368. $this->_[HDOM_INFO_SPACE][] = array(' ', '', '');
  369. $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
  370. }
  371. $this->attr[$name] = $value;
  372. }
  373. function __isset($name) {
  374. switch($name) {
  375. case 'outertext': return true;
  376. case 'innertext': return true;
  377. case 'plaintext': return true;
  378. }
  379. //no value attr: nowrap, checked selected...
  380. return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]);
  381. }
  382. function __unset($name) {
  383. if (isset($this->attr[$name]))
  384. unset($this->attr[$name]);
  385. }
  386. // camel naming conventions
  387. function getAllAttributes() {return $this->attr;}
  388. function getAttribute($name) {return $this->__get($name);}
  389. function setAttribute($name, $value) {$this->__set($name, $value);}
  390. function hasAttribute($name) {return $this->__isset($name);}
  391. function removeAttribute($name) {$this->__set($name, null);}
  392. function getElementById($id) {return $this->find("#$id", 0);}
  393. function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
  394. function getElementByTagName($name) {return $this->find($name, 0);}
  395. function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}
  396. function parentNode() {return $this->parent();}
  397. function childNodes($idx=-1) {return $this->children($idx);}
  398. function firstChild() {return $this->first_child();}
  399. function lastChild() {return $this->last_child();}
  400. function nextSibling() {return $this->next_sibling();}
  401. function previousSibling() {return $this->prev_sibling();}
  402. }
  403. // simple html dom parser
  404. // -----------------------------------------------------------------------------
  405. class simple_html_dom {
  406. public $root = null;
  407. public $nodes = array();
  408. public $callback = null;
  409. public $lowercase = false;
  410. protected $pos;
  411. protected $doc;
  412. protected $char;
  413. protected $size;
  414. protected $cursor;
  415. protected $parent;
  416. protected $noise = array();
  417. protected $token_blank = " \t\r\n";
  418. protected $token_equal = ' =/>';
  419. protected $token_slash = " />\r\n\t";
  420. protected $token_attr = ' >';
  421. // use isset instead of in_array, performance boost about 30%...
  422. protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);
  423. protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);
  424. protected $optional_closing_tags = array(
  425. 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),
  426. 'th'=>array('th'=>1),
  427. 'td'=>array('td'=>1),
  428. 'li'=>array('li'=>1),
  429. 'dt'=>array('dt'=>1, 'dd'=>1),
  430. 'dd'=>array('dd'=>1, 'dt'=>1),
  431. 'dl'=>array('dd'=>1, 'dt'=>1),
  432. 'p'=>array('p'=>1),
  433. 'nobr'=>array('nobr'=>1),
  434. );
  435. function __construct($str=null) {
  436. if ($str) {
  437. if (preg_match("/^http:\/\//i",$str) || is_file($str))
  438. $this->load_file($str);
  439. else
  440. $this->load($str);
  441. }
  442. }
  443. http://www.devdao.com/
  444. function __destruct() {
  445. $this->clear();
  446. }
  447. // load html from string
  448. function load($str, $lowercase=true) {
  449. // prepare
  450. $this->prepare($str, $lowercase);
  451. // strip out comments
  452. $this->remove_noise("''is");
  453. // strip out cdata
  454. $this->remove_noise("''is", true);
  455. // strip out
复制代码

tqq.php

  1. /**

  2. Version: 1.11 ($Rev: 175 $)
  3. */
  4. //缓存时间,单位:秒
  5. $t = 360;
  6. if(!is_file('index.html')||(time()-filemtime('index.html'))>$t){
  7. //微博帐号
  8. $qq = 'kuaisubeian';
  9. //经过腾讯那个md5_3()加密后的密码
  10. $pwd = '624D3274815F2237817A7C62F42DD26A';
  11. $verifyURL = 'http://ptlogin2.qq.com/check?uin=@'.$qq.'&appid=46000101';
  12. $loginURL = 'http://ptlogin2.qq.com/login?';
  13. //获取验证码及第一次cookie

  14. $curl = curl_init($verifyURL);
  15. $cookie_jar = tempnam('.', 'cookie');
  16. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  17. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  18. $verifyCode = curl_exec($curl);
  19. curl_close($curl);
  20. $verifyCode = strtoupper(substr($verifyCode, 18, 4));
  21. //echo '验证码:'.$verifyCode;
  22. //echo '
    ';
  23. // echo 'Cookies:'.$cookie_jar;
  24. // echo '
    ';
  25. //发送登录请求并获取第二次cookie

  26. $loginURL .= 'u=@'.$qq.'&p='.md5($pwd.$verifyCode).'&verifycode='.$verifyCode.'&aid=46000101&u1=http%3A%2F%2Ft.qq.com&h=1&from_ui=1&fp=loginerroralert';
  27. //echo '登录地址:'.$loginURL;
  28. //echo '
    ';
  29. $curl = curl_init($loginURL);
  30. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  32. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  33. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  34. $loginResult = curl_exec($curl);
  35. curl_close($curl);
  36. //echo '登录验证结果:'.$loginResult;
  37. //echo '
    ';
  38. http://bbs.it-home.org
  39. //获取第三次cookie
  40. $curl = curl_init('http://t.qq.com');
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  43. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  44. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  45. $loginResult = curl_exec($curl);
  46. curl_close($curl);
  47. //第四次

  48. $curl = curl_init('http://t.qq.com/'.$qq.'/mine');
  49. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  51. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  52. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  53. $loginResult = curl_exec($curl);
  54. curl_close($curl);
  55. unlink($cookie_jar);

  56. file_put_contents('index.html',$loginResult);
  57. }
  58. include('cnz.php');
  59. $html = file_get_html('index.html');
  60. $talkList = $html->find('#talkList');
  61. $lastTalk = $talkList[0];
  62. $userName = $lastTalk->children(0)->children(1)->find('.userName');

  63. $msgCnt = $lastTalk->children(0)->children(1)->find('.msgCnt');
  64. $pubInfo = $lastTalk->children(0)->children(1)->find('.pubInfo');
  65. $userName = $userName[0]->plaintext;

  66. $result = '';

  67. //大于二则是转播

  68. if(count($msgCnt) $pi = $pubInfo[0]->find('.left');
  69. $result = $userName.$msgCnt[0]->plaintext.'

  70. QQweiboQQ|www.beiantuan.com '.$pi[0]->children(0)->plaintext.' '.$pi[0]->children(1)->plaintext.'';
  71. }else{
  72. $pi = $pubInfo[1]->find('.left');
  73. $result = $userName.$msgCnt[0]->plaintext.'['.$msgCnt[1]->plaintext.'] '.$pi[0]->plaintext.'';
  74. }
  75. echo $result;
  76. ?>
复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal