当前位置:Gxlcms > PHP教程 > php实例之新浪星座运势

php实例之新浪星座运势

时间:2021-07-01 10:21:17 帮助过:29人阅读

例子,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('/(.*?)(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)(.*?)<\/div>/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], 'unset($matches);
  25. $array=json_decode(file_get_contents($astro),true);
  26. }
  27. //明天的日期
  28. $mingtian=date('Ymd',strtotime('+1 day'));
  29. foreach(array($constellation) as $astro){
  30. preg_match('/(.*?)(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)(.*?)<\/div>/isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'.$mingtian.''), $matches);
  31. //这样正则,可以更小的受新浪改版所带来的影响

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

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

人气教程排行