Home  >  Article  >  Backend Development  >  PHP example of Sina horoscope

PHP example of Sina horoscope

WBOY
WBOYOriginal
2016-07-25 09:13:151663browse

例子,php获取新浪星座与运势。

  1. function html2txt($document){

  2. $search = array('@]*?>.*?@si', // Strip out javascript
  3. '@<[/!]*?[^<>]*?>@si', // Strip out HTML tags
  4. '@]*?>.*?@siU', // Strip style tags properly
  5. '@@', // Strip multi-line comments including CDATA
  6. '@s+| @'
  7. );
  8. $text = preg_replace($search, '', $document);
  9. return $text;
  10. } bbs.it-home.org
  11. function curl($url){
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_URL, $url);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($ch, CURLOPT_REFERER, 'http://astro.sina.com.cn/fate/astro_aries.html?prourl=0');//伪造来源
  16. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/'.mt_rand(2, 9).'.0 (Windows NT 6.1; WOW64; rv:'.mt_rand(10, 30).'.0) Gecko/'.mt_rand(2000, 2014).'0101 Firefox/'.mt_rand(10, 30).'.0');//随机header
  17. curl_setopt($ch, CURLOPT_HEADER, 0);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. return $data;
  21. }
  22. foreach(array($constellation) as $astro){
  23. preg_match('/(.*?)(.*?).*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)
    (.*?)
    /isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'), $matches);
  24. file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], '
  25. unset($matches);
  26. $array=json_decode(file_get_contents($astro),true);
  27. }
  28. //明天的日期
  29. $mingtian=date('Ymd',strtotime('+1 day'));
  30. foreach(array($constellation) as $astro){
  31. preg_match('/(.*?)(.*?).*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)
    (.*?)
    /isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'.$mingtian.''), $matches);
  32. //这样正则,可以更小的受新浪改版所带来的影响

  33. file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], 'unset($matches);

  34. $array2=json_decode(file_get_contents($astro),true);
  35. }
  36. //今天的运势
  37. print_r($array)
  38. //明天的运势
  39. print_r($array2)
  40. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn