php实例之新浪星座运势

WBOY
发布: 2016-07-25 09:13:15
原创
1589 人浏览过

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

  1. function html2txt($document){

  2. $search = array('@<script>]*?>.*?</script>@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], 'php实例之新浪星座运势 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('/(.*?)(.*?).*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)
    (.*?)/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], 'php实例之新浪星座运势 unset($matches);

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


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!