首頁  >  文章  >  後端開發  >  php使用騰訊QQ微博API介面取得微博內容的程式碼

php使用騰訊QQ微博API介面取得微博內容的程式碼

WBOY
WBOY原創
2016-07-25 08:57:471225瀏覽
  1. /**

  2. * 使用騰訊QQ微博API介面取得微博內容
  3. * by bbs.it-home.org
  4. */
  5. define('HDOM_TYPE_ELEMENT', 1);
  6. define('HDOM_TYPE_ELEMENT', 1);
  7. define('HDOM_TYPE_ELEMENT', 1);
  8. define('HDOM_TYPE_ELEMENT', 1);
  9. Define('HDOM_TYPE_COMMENT', 2);
  10. define('HDOM_TYPE_TEXT', 3);
  11. define('HDOM_TYPE_ENDTAG', 4);
  12. define('HDOM_TYPE_ENDTA', 5)RO; 'HDOM_TYPE_UNKNOWN', 6);
  13. define('HDOM_QUOTE_DOUBLE', 0);
  14. define('HDOM_QUOTE_SINGLE', 1);
  15. define('HDOM_QUOTE_NO', . ', 0);
  16. define('HDOM_INFO_END', 1);
  17. define('HDOM_INFO_QUOTE', 2);
  18. define('HDOM_INFO_SPACE', 3);
  19. define(DOMOM_IN 4);
  20. define('HDOM_INFO_INNER', 5);
  21. define('HDOM_INFO_OUTER', 6);
  22. define('HDOM_INFO_ENDSPACE',7);
  23. //輔助函數
  24. / ---------------
  25. //取得html dom 表單檔案
  26. function file_get_html() {
  27. $dom = new simple_html_dom;
  28. $args = func_get_args ();
  29. $dom->load(call_user_func_array('file_get_contents', $args), true);
  30. return $dom;
  31. }
  32. //取得html dom表單字串
  33. function str_get_html($str, $lowercase=true) {
  34. $dom = new simple_html_dom;
  35. $dom->load($str, $lowercase);
  36. return $dom;
  37. }
  38. // 轉儲html dom 樹
  39. function dump_html_tree($node, $show_attr=true, $deep=0) {
  40. $lead = str_repeat(' ', $deep);
  41. echo $ Lead. $node->tag;
  42. if ($show_attr && count($node->attr)>0) {
  43. echo '(';
  44. foreach($node->attr as $k=>$ v)
  45. echo "[$k]=>"".$node->$k.'", ';
  46. echo ')';
  47. }
  48. echo "n";
  49. foreach($node->nodes as $c)
  50. dump_html_tree($c, $show_attr, $deep+1);
  51. }
  52. //取得dom表單檔案(已棄用)
  53. function file_get_dom() {
  54. $dom = new simple_html_dom;
  55. $args = func_get_args();
  56. $dom->load(call_user_func_array('file_get_contents', $args) ), true); return $dom;
  57. }
  58. //取得dom 形式字串(已棄用)
  59. function str_get_dom($str, $lowercase=true) {
  60. $dom = new simple_html_dom;
  61. $dom->load($str, $lowercase);
  62. return $dom;
  63. }
  64. //簡單html dom 節點
  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. // 由於php5 循環而清理記憶體引用記憶體洩漏...
  86. function clear () {
  87. $this->dom = null;
  88. $this->nodes = null;
  89. $this->parent = null;
  90. $this->children = null;
  91. }
  92. // 轉儲節點的樹

  93. function dump($show_attr=true) {
  94. dump_html_tree($ this, $show_attr);
  95. }
  96. // 回傳節點
  97. 的父節點function Parent() {
  98. return $this->parent;
  99. }
  100. // 回傳節點
  101. 的子節點function child($idx=-1) {
  102. if ($idx==-1 ) return $this->children;
  103. if (isset($this->children[$idx] ) )) return $this->children[$idx];
  104. return null;
  105. }
  106. // 傳回節點
  107. 的第一個子節點function first_child() {
  108. if ( count ($this->children)>0) return $this->children[0];
  109. return null;
  110. }
  111. // 回傳節點
  112. 的最後一個子節點function last_child() {
  113. if (($count=count($this->children))>0) return $this->children[$count-1];
  114. return null;
  115. }
  116. //回傳節點
  117. 的下一個兄弟節點function next_sibling() {
  118. if ($this ->parent===null) return null;
  119. $idx = 0;
  120. $count = count($ this ->父級->子級);
  121. while ($idx父級->子級[$idx])
  122. ++$idx;
  123. if ( ++$idx>=$count) return null;
  124. return $this->parent->children[$idx];
  125. }
  126. // 回傳節點
  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. // 取得dom節點的內部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. // 取得dom 節點的外部文字(帶標籤)
  145. 函數externaltext() {
  146. if ($this ->tag==='root') return $this->innertext();
  147. // 觸發回呼
  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. // 渲染開始標籤
  153. $ret = $this->dom-> nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();
  154. // 渲染內部文字
  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. / / 渲染結束標記
  162. if(isset($this->_[HDOM_INFO_END) ]) && $this->_[HDOM_INFO_END]!=0)
  163. $ret .= ''.$this-> tag.'>';
  164. return $ret;
  165. }
  166. // 取得dom 節點的純文字
  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_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 ''===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. // 使用標籤建立節點文字
  187. function Makeup() {
  188. //文字、註解、未知
  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. // 跳過已刪除的屬性
  195. if ($val===null | | $val===false)
  196. continue;
  197. $ret .= $this->_[HDOM_INFO_SPACE ][$i][0];
  198. //無值屬性:nowrap,選取所選...
  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. 預設值:$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. 回傳$ret 。 $this->_[HDOM_INFO_ENDSPACE] 。 '>';
  212. }
  213. // 透過CSS 選擇器找出元素
  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. // 找出每個選擇器
  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. // 處理後代選擇器,無遞歸!
  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. // 排序鍵
  236. ksort($found_keys);
  237. $found = array();
  238. foreach($found_keys as $k=>$v)
  239. $found[] = $this->dom->nodes[$k];
  240. // 傳回第n 個元素或陣列
  241. if (is_null($idx)) return $found;
  242. else if ($idx return (isset($found[ $idx])) ? $found[$idx] : null;
  243. }
  244. // 找給定條件
  245. protected functioneek($selector, &$ret) {
  246. list($tag, $key, $val , $exp, $no_key) = $selector;
  247. // xpath 索引
  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==$鍵) {
  253. $ret[$c->_[HDOM_INFO_BEGIN]] = 1;
  254. return;
  255. }
  256. }
  257. }
  258. return;
  259. }
  260. $endend = (!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. // 比較標籤
  277. if ($tag && $tag! =$node->tag && $tag!=='*') {$pass=false;}
  278. // 比較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. // 比較值
  286. if ($pass && $key && $val && $val!=='*') {
  287. $ check = $this- >match($exp, $val, $node->attr[$key]);
  288. // 處理多個類別
  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. 受保護函數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. // CSS 選擇器的模式,從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] = trimrim ($ m[0]);
  327. if ($m[0]==='' || $m[0]==='/' || $m[0]==='//' ) 繼續;
  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. // 轉換成小寫
  337. if ($this->dom->lowercase) {$tag=strtolower($tag); $key= strtolower($key);}
  338. //不具有指定屬性的元素
  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. $選擇器[] = $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. 預設值: 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': 回傳true;
  376. case 'innertext': 回傳true;
  377. ;
  378. case 'plaintext': 回傳true;
  379. }
  380. //無值attr:nowrap,選取選定...
  381. return (array_key_exists($name, $this->attr)) ? true : isset ($this->attr[$name]);
  382. }
  383. function __unset($name) {
  384. if (isset($this->attr[$name]))
  385. unset($ this->attr[$name]);
  386. }
  387. // 駱駝命名約定
  388. function getAllAttributes() {return $this->attr;}
  389. function getAttribute ($name) {return $attr;}
  390. function getAttribute ($name) {return $ this->__get($name);}
  391. function setAttribute($name, $value) {$this->__set($name, $value);}
  392. function hasAttribute($name) {return $this ->__isset($name);}
  393. function removeAttribute($name) {$this->__set($name, null);}
  394. function getElementById($ id) {return $this->find(" #$id", 0);}
  395. function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
  396. function getElementByTagName($name ) {return $this->find($name, 0);}
  397. function getElementsByTagName($name, $idx=null) {return $this->find($name, 0);}
  398. function getElementsByTagName ($name, $idx=null) {return $this-> ;find($name, $idx);}
  399. function ParentNode() {return $this->parent();}
  400. function childNodes( $idx=-1) {return $this->children ($idx);}
  401. function firstChild() {return $this->first_child();}
  402. function lastChild() {return $this-> last_child();} function nextSibling( ) {return $this->next_sibling();} function previousSibling() {return $this->prev_sibling();}}
  403. // 簡單的 html dom 解析器
  404. // ---------------------------------- -------------------------------------------------- ------
  405. class simple_html_dom {
  406. public $root = null;
  407. public $nodes = array();
  408. public $callback = null;
  409. public $lowercase = false;
  410. 受保護的$pos;
  411. 受保護的$doc;
  412. 受保護的$char;
  413. 受保護的$size;
  414. 受保護的$cursor;
  415. 受保護的$parent;
  416. 受保護的$noise = 陣列();
  417. protected $token_blank = " trn";
  418. protected $token_equal = ' =/>';
  419. protected $token_slash = " / >rnt";
  420. protected $token_attr = ' >' ;
  421. // 使用isset 而不是in_array,效能提升約30%...
  422. protected $self_ending_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_ending_tags = array(
  425. 'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),
  426. 'th'= >array('th'= >1),
  427. 'td'=>陣列('td'=>1),
  428. 'li'=>陣列('li'= >; 1),
  429. 'dt'=>陣列('dt'=>1, 'dd'=>1),
  430. 'dd'=>陣列('dd'=>1, 'dt' =>1),
  431. 'dl'=>陣列('dd'=>1, 'dt'=>1),
  432. 'p'=>陣列('p '=>1),
  433. 'nobr'=>陣列('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. // 從字串載入html
  448. function load($str, $lowercase=true) {
  449. // 準備
  450. $this-> ; prepare($str, $lowercase);
  451. // 刪除註解
  452. $this->remove_noise("''is");
  453. // 刪除cdata
  454. $this->remove_noise("''is", true);
  455. // 去掉
  456. 風格>標籤
  457. $this- >remove_noise("']*[^/]>(.*?)'is") ;
  458. $this->remove_noise("'(.*?)'is");
  459. // 去掉標籤
  460. $this->remove_noise("']*[^/]>(.*?)'is") ;
  461. $this->remove_noise("'(.*? )'is");
  462. // 刪除預篩選標籤
  463. $this->remove_noise("']*> ( .*?) ;'is");
  464. // 刪除伺服器端腳本
  465. $this->remove_noise("'()( . *?)(?>)'s", true);
  466. // 分割 smarty 腳本
  467. $this->remove_noise("'({w)(.*?)(})'s", true);
  468. // 解析
  469. while ( $ this->parse());
  470. // 結束
  471. $this->root->_[HDOM_INFO_END] = $this->cursor;
  472. }
  473. // 載入來自檔案的html
  474. function load_file() {
  475. $args = func_get_args();
  476. $this->load(call_user_func_array('file_get_contents', $args), true);
  477. }
  478. }
  479. // 設定回復函數
  480. function set_callback($function_name) {
  481. $this->callback = $function_name;
  482. }
  483. // 刪除回呼函數
  484. function remove_callback() { callback = null;
  485. }
  486. // 將dom 儲存為字串
  487. function save($filepath='') {
  488. $ret = $this->root- >innertext();
  489. if ($filepath!=='') file_put_contents($filepath, $ret);
  490. return $ret;
  491. }
  492. // 透過css選擇器來尋找🎜> function find($selector, $idx=null) {
  493. return $this->root->find($selector, $idx);
  494. }
  495. // 清理記憶體php5 迴圈引用記憶體浪費.. .
  496. function clear() {
  497. foreach($this->nodes as $n) {$n->clear(); $n = null;}
  498. if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
  499. if (isset($this->root)) {$this->root->clear();取消設定($this->root);}
  500. 取消設定($this->doc);
  501. 取消設定($this->雜訊);
  502. }
  503. function dump($show_attr=true) {

  504. $this->root->dump($show_attr);
  505. }
  506. // 準備 HTML 資料並初始化所有內容
  507. protected function prepare($ str, $lowercase=true) {
  508. $this->clear();
  509. $this->doc = $str;
  510. $this->pos = 0 ;
  511. $this ->cursor = 1;
  512. $this->noise = array();
  513. $this->nodes = array();
  514. $this->lowercase = $lowercase;
  515. $this->root = new simple_html_dom_node($this);
  516. $this->root->tag = 'root';
  517. $this->root->_[HDOM_INFO_BEGIN] = -1;
  518. $this->root->nodetype = HDOM_TYPE_ROOT;
  519. $this->parent = $this->root;
  520. // 設定內容的長度
  521. $ this->size = strlen($ str);
  522. if ($this->size>0) $this->char = $this->doc[0];
  523. }
  524. // 解析html 內容
  525. protected function parse( ) {
  526. if (($s = $this->copy_until_char(' return $this-> ;read_tag();
  527. // 文字
  528. $node = new simple_html_dom_node($this);
  529. ++$this->曼谷;
  530. $node->_[HDOM_INFO_TEXT] = $ s;
  531. $this->link_nodes($node, false) ;
  532. return true;
  533. }
  534. // 讀取標籤資訊
  535. protected function read_tag() {
  536. if ( $this->char!==' $this->root->_[HDOM_INFO_END] = $this->遊標;
  537. return false;
  538. }
  539. $begin_tag_pos = $this->pos;
  540. $this->char = ( ++$this->possize) ? $this->doc[$this->pos] : null; // 下一個
  541. // 結束標記
  542. if ($this->char==='/') {
  543. $this->char = (++$this->posdoc[$this->pos] : null; // next
  544. $this->skip($this->token_blank_t);
  545. $tag = $this->copy_until_char( '>') ;
  546. // 跳過結束標記中的屬性
  547. if (($pos = strpos($tag, ' '))!==false)
  548. $tag = substr($tag , 0, $ pos);
  549. $parent_lower = strtolower($this ->parent->tag);
  550. $tag_lower = strtolower($tag);
  551. if ($parent_lower!==$tag_lower) {
  552. if (isset($this->optical_writing_tags[ $parent_lower]) && isset($this->block_tags[$tag_lower])) {
  553. $this->parent->_[HDOM_INFO_END] = 0
  554. $this->parent->_[HDOM_INFO_END] = 0
  555. $🎜> $ org_parent = $this->;父親;
  556. while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)
  557. $ this->parent = $ this->parent->parent;
  558. if (strtolower($this->parent->tag)!==$tag_lower) {
  559. $this->parent = $org_parent; / / 恢復原始父級
  560. if ($this->parent->parent) $this->parent = $this->parent->parent;
  561. $this->parent-> ;_[HDOM_INFO_END] = $this->曼谷;
  562. return $this->as_text_node($tag);
  563. }
  564. }
  565. else if (($this->parent->parent) && isset($this- >block_tags[$ tag_lower])) {
  566. $this->parent->_[HDOM_INFO_END] = 0;
  567. $org_parent = $this->;父;
  568. while (($this->parent ->parent) && strtolower($this->parent->tag)!==$tag_lower)
  569. $this->parent = $ this->parent->parent;
  570. if (strtolower($this- >parent-> tag)!==$tag_lower) {
  571. $this->parent = $org_parent; // 恢復原始父級
  572. $this->parent->_[HDOM_INFO_END] = $this->cursor;
  573. return $this->as_text_node($tag);
  574. }
  575. }
  576. else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower) {
  577. $this ->parent->_[HDOM_INFO_END ] = 0;
  578. $this->parent = $this->parent->parent;
  579. }
  580. else
  581. return $this->as_text_node($tag); }
  582. $this->parent->_[HDOM_INFO_END] = $this->cursor;
  583. if ($this->parent->parent) $this->parent = $this->parent->parent;
  584. $this->char = (++$this->possize) ? $this->doc[$this->pos] : null; // next
  585. 回傳 true;
  586. }
  587. $node = new simple_html_dom_node($this);
  588. $node->_[HDOM_INFO_BEGIN] = $this->cursor;
  589. + +$ this->cursor;
  590. $tag = $this->copy_until($this->token_slash);
  591. // doctype、cdata 與註解...
  592. if (isset($ tag[0]) && $tag[0]==='!') {
  593. $node->_[HDOM_INFO_TEXT] = ' $node->nodetype = HDOM_TYPE_COMMENT;
  594. $node- >tag = 'comment';
  595. } else {
  596. $node->nodetype = HDOM_TYPE_UNKNOWN;
  597. $ node->tag = 'unknown';
  598. }
  599. if ($this-> char==='>') $node->_[HDOM_INFO_TEXT].='>';
  600. $this->link_nodes($node, true);
  601. $this->char = (++$這個->possize) ? $this->doc[$this->pos] : null; // next
  602. return true;
  603. }
  604. // text
  605. if ($pos=strpos($tag, ' $tag = ' doc[--$this->pos]; // prev
  606. return true;
  607. }
  608. if (!preg_match("/^[w-:]+$/", $tag)) {
  609. $node->_[HDOM_INFO_TEXT ] = ' return true;
  610. }
  611. if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';
  612. $ this- >link_nodes($node, false);
  613. $this->char = (++$this->possize) ? $this->doc[$this->pos] : null; // next
  614. return true;
  615. }
  616. // 開始標籤
  617. $node->nodetype = HDOM_TYPE_ELEMENT;
  618. $tag_lower = strtolower($tag); $no🎜> $node-標籤= ($this->小寫) ? $tag_lower : $tag;
  619. // 處理任選的結束標籤
  620. if (isset($this->optical_writing_tags[$tag_lower]) ) {
  621. while (isset($this->optical_ending_tags[$ tag_lower][strtolower($this->parent->tag)])) {
  622. $this->parent->_[HDOM_INFO_END] = 0;
  623. $this->parent = $this->parent- >parent;
  624. }
  625. $node->parent = $this->parent;
  626. }
  627. $guard = 0; // 防止無限迴圈
  628. $space = array($this->copy_skip($this->token_blank), '', '');
  629. // 屬性
  630. do {
  631. if ($ this->char!==null && $space[0]==='') break;
  632. $name = $this->copy_until($this->token_equal);
  633. if ($guard== =$this->pos) {
  634. $this->char = (++$this->possize) ? $this->doc[$this->pos] : null; // next
  635. continue;
  636. }
  637. $guard = $this->pos;
  638. // 處理無限的 ' if($this->pos>=$ this- >size-1 && $this->char!=='>') {
  639. $node->nodetype = HDOM_TYPE_TEXT;
  640. $node->_[HDOM_INFO_END] = 0;
  641. $node->_[HDOM_INFO_TEXT] = ' 回傳true;
  642. }
  643. // 處理不符 ' if($this->doc[$this->pos-1]==' $node->nodetype = HDOM_TYPE_TEXT;
  644. $node->tag = 'text';
  645. $node->attr = array();
  646. $node->_[HDOM_INFO_END] = 0;
  647. $node ->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1);
  648. $this->pos -= 2;
  649. $this->char = (++$this- $this->doc[$this->pos] : null; >possize) ? // next
  650. $this->link_nodes($node, false);
  651. return true;
  652. }
  653. if ($name!=='/' && $name!=='' ) {
  654. $space[1] = $this->copy_skip($this->token_blank);
  655. $name = $this->restore_noise($name);
  656. if ($this ->小寫) $name = strtolower($name);
  657. if ($this->char==='=') {
  658. $this->char = (++$this-> ;pos尺寸) ? $this->doc[$this->pos] : null; // next
  659. $this->parse_attr($node, $name, $space);
  660. }
  661. else {
  662. //無值屬性:nowrap,勾選...
  663. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
  664. $node->attr[$name] = true;
  665. if ($this->char!='>') $這-> char = $這-> doc[--$這-> pos]; // 上一個
  666. }
  667. $node->_[HDOM_INFO_SPACE][] = $space;
  668. $space = array($this->copy_skip($this->token_blank), '' , ' ');
  669. }
  670. else
  671. 規則;
  672. } while($this->char!=='>' && $this->char!=='/' );
  673. $this->link_nodes($node, true);
  674. $node->_[HDOM_INFO_ENDSPACE] = $space[0];
  675. // 檢查自關閉
  676. if ( $this->copy_until_char_escape(' >')==='/') {
  677. $node->_[HDOM_INFO_ENDSPACE] .= '/';
  678. $node->_[HDOM_INFO_END] = 0;
  679. }
  680. else {
  681. // 重置父級
  682. if (!isset($this->self_ending_tags[strtolower($node->tag)])) $this-> ;parent = $node;
  683. }
  684. $this->char = (++$this->possize) ? $this->doc[$this->pos] : null; // next
  685. return true;
  686. }
  687. // 解析屬性
  688. protected function parse_attr($node, $name, &$space) {
  689. $space[2] = $this- >copy_skip($this->token_blank);
  690. switch($this->char) {
  691. case '"':
  692. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE; this->char = (++$this->possize) ? $this->doc[$this->pos] : null; > $node->attr[$name] = $this- >restore_noise($this->copy_until_char_escape('"'));
  693. $this->char = (++$this->; possize) ? $this->doc[$this->pos] : null; // next
  694. break;
  695. case ''':
  696. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;
  697. $this->char = (++$this-> ;pos 大小) ? $this->doc[$this->pos] : null; // next
  698. $node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('''));
  699. $this->char = (++ $this-> pos大小) ? $this->doc[$this->pos] : null; // next
  700. break;
  701. 預設:
  702. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
  703. $node->attr[$name] = $this->s_noise (thisrestore_noise ($thisrestore_noise) (thisrestore_noise (thisrestore_noise) ->copy_until($this->token_attr));
  704. }
  705. }
  706. // 連結節點的父節點
  707. protected function link_nodes(&$node, $is_child) {
  708. $nonode ->parent = $this->parent;
  709. $this->parent->nodes[] = $node;
  710. if ($is_child)
  711. $this->; Parent->children[] = $node;
  712. }
  713. // 作為文字節點
  714. protected function as_text_node($tag) {
  715. $node = new simple_html_dom_node($this);
  716. $node = new simple_html_dom_node($this);
  717. + +$this-> 曼谷;
  718. $node->_[HDOM_INFO_TEXT] = '' 。 this->char = (++$this->possize) ? $this->doc[$this->pos] : null; // 下一個
  719. 回傳 true;
  720. }
  721. 受保護的函數skip($chars) {
  722. $this->pos += strspn($this->doc, $chars, $this->pos);
  723. $this->; char = ($this->possize) ? $this->doc[$this->pos] : null; // next
  724. }
  725. 受保護函數copy_skip($chars) {
  726. $pos = $this->pos;
  727. $len = strspn($this->doc, $chars, $ pos ) ;
  728. $this->pos += $len;
  729. $this->char = ($this->possize) ? $this->doc[$this->pos] : null; // next
  730. if ($len===0) return '';
  731. return substr($this->doc, $pos, $len);
  732. }
  733. protected function copy_until( $ chars) {
  734. $pos = $this->pos;
  735. $len = strcspn($this->doc, $chars, $pos);
  736. $this->pos += $len;
  737. $this->char = ($this->possize) ? $this->doc[$this->pos] : null; // next
  738. return substr($this->doc, $pos, $len);
  739. }
  740. protected function copy_until_char($char) {
  741. if ($this->char= == null) return '';
  742. if (($pos = strpos($this->doc, $char, $this->pos))===false) {
  743. $ret = substr ($this- >doc, $this->pos, $this->size-$this->pos);
  744. $this->char = null;
  745. $this->; pos = $this->size;
  746. return $ret;
  747. }
  748. if ($pos===$this->pos) return '';
  749. $pos_old = $this->pos ;
  750. $this->char = $this->doc[$pos];
  751. $this->pos = $pos;
  752. return substr($this->doc , $pos_old, $pos- $pos_old);
  753. }
  754. protected function copy_until_char_escape($char) {
  755. if ($this->char===null) return '';
  756. $ start = $this->pos;
  757. while(1) {
  758. if (($pos = strpos($this->doc, $char, $start))===false) {
  759. $ret = substr($this-> doc, $this->pos, $this->size-$this->pos);
  760. $this->char = null;
  761. $this->pos = $this->size;
  762. return $ret;
  763. }
  764. if ($pos===$this->pos) return '';
  765. if ($this->doc[$pos-1]==='\' ) {
  766. $start = $pos+1;
  767. 繼續;
  768. }
  769. $pos_old = $this- >pos;
  770. $this->char = $this->doc[$ pos];
  771. $this->pos = $pos;
  772. return substr($this->doc , $pos_old, $pos-$pos_old);
  773. }
  774. }
  775. //去除html內容中的噪音
  776. protected function remove_noise($pattern, $remove_tag=false) {
  777. $count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPd);對於($i=$count-1; $i>-1; --$i) {
  778. $key = '___noise___'.sprintf('% 3d', count($this->noise)+100);
  779. $idx = ($remove_tag) ? 0 : 1;
  780. $this->noise[$key] = $matches[$i][$idx][0];
  781. $this->doc = substr_replace($this->doc , $key , $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));
  782. }
  783. // 重設內容長度
  784. $ this->size = strlen($this->doc);
  785. if ($this->size>0) $this->char = $this->doc[0] ;
  786. }
  787. / / 將雜訊還原到html 內容
  788. function restore_noise($text) {
  789. while(($pos=strpos($text, '___noise___'))!==false) {
  790. $key = '___noise___ '.$text[$pos+11].$text[$pos+12].$text[$pos+13];
  791. if (isset($this->noise) [$key]))
  792. $text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+14);
  793. }
  794. return $text;
  795. }
  796. function __toString() {
  797. return $this->root->innertext();
  798. }
  799. function __get($name) {
  800. switch($name) {
  801. case 'outertext': return $this->root->innertext();
  802. case 'innertext': return $this->root->innertext( );
  803. case 'plaintext': return $this-> root->text();
  804. }
  805. }
  806. // 駱駝起源約定
  807. function childNodes($idx=- 1) {return $this->root->childNodes($idx); }
  808. function firstChild() {return $this->root->first_child();}
  809. function lastChild() {return $this->root->last_child();}
  810. function getElementById($ id) {return $this->find("#$id", 0);}
  811. function getElementsById( $id, $idx=null) {return $this->find("#$id", $idx );}
  812. function getElementByTagName($name) {return $this->find($name, 0 );}
  813. function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);}
  814. function loadFile() {$args = func_get_args();$this-> load(call_user_func_array('file_get_contents', $args), true);}
  815. }
  816. ?>
複製程式碼

複製程式碼
  1. tqq.php
  2. /**

  3. 版本:1.11($Rev:175 $)
  4. */
  5. //快取時間,單位:秒
  6. $t = 360;
  7. if(!is_file('index.html')||(time()-filemtime('index.html'))>$t){
  8. //微博帳號
  9. $qq = 'kuaisubeian';
  10. //經過騰訊那個md5_3()加密後的密碼
  11. $pwd = '624D3274815F2237817A7C62F42DD26A'; $loginURL = 'http://ptlogin2.qq.com/login?';
  12. / /取得驗證碼及第一次cookie

  13. $curl = curl_init($verifyURL);
  14. $cookie_jar = tempnam('.', 'cookie');
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 11) ;
  16. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  17. $verifyCode = curl_exec($curl);
  18. curl_close($curl);
  19. $verifyCode = strver. , 4));
  20. //echo '驗證碼:'.$verifyCode;
  21. //echo '
    ';
  22. // echo 'Cookies:'.$cookie_jar;
  23. // echo '
    ';
  24. //發送登入要求並取得第二次cookie

  25. $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'; //echo '
    ';
  26. $curl = curl_init($loginURL);
  27. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); ($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  28. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  29. curl_setopt($curl, CURLOPT_CIEJAR, $cookie;
  30. curl_close($curl);
  31. //echo '登入驗證結果:'.$loginResult;
  32. //echo '
    ';
  33. http://bbs.it-home .org
  34. //取得第三次cookie
  35. $curl = curl_init('http://t.qq.com');
  36. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  37. curl_setopttopt ($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  38. curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
  39. curl_setopt($curl, CURLOPT_CIEJAR, $cookie;
  40. curl_close($curl);
  41. //第四次

  42. $curl = curl_init('http://t.qq.com/'.$qq.'/ mine');
  43. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  44. curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
  45. curl_setop($c), CURL); $curl, CURLOPT_COOKIEJAR, $cookie_jar);
  46. $loginResult = curl_exec($curl);
  47. curl_close($curl);
  48. unlink($cookie_jar);}

  49. include('cnz.php');
  50. $html = file_get_html('index.html');
  51. $talkList = $html ->find('#talkList');
  52. $lastTalk = $talkList[0];
  53. $userName = $lastTalk->children(0)->children(1)- >find('.userName');

  54. $msgCnt = $lastTalk->children(0)->children(1)->find('.msgCnt');
  55. $pubInfo = $lastTalk->children (0)->children(1)->find('.pubInfo');
  56. $userName = $userName[0]->plaintext;

  57. $result = '';

  58. //大於二則是轉播

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

  61. QQweiboQQ|www.beiantuan.com '.$pi[0]->children(0)->plaintext.' '.$pi[0 ]->children(1)->plaintext.'';
  62. }else{
  63. $pi = $pubInfo[1]->find('.left');
  64. $result = $userName.$msgCnt[0]->plaintext.'['.$msgCnt[1]->plaintext.'] '.$pi[0]->plaintext.'';
  65. }
  66. echo $result;
  67. ?>
複製程式碼
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn