Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php实例之新浪星座运势

php实例之新浪星座运势

发布时间:2018-04-24   编辑:www.jquerycn.cn
本文介绍了php获取新浪星座与运势的方法,利用php获取新浪的星座运势,可获取今天、明天的星座运势,使用php接口获取过来的数据根据需要替换一下内容即可,有需要的朋友参考下。

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

复制代码 代码示例:

<?php
function html2txt($document){
    $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
            '@<[\/\!]*?[^<>]*?>@si',           // Strip out HTML tags
            '@<style[^>]*?>.*?</style>@siU',   // Strip style tags properly
            '@<![\s\S]*?--[ \t\n\r]*>@',       // Strip multi-line comments including CDATA
            '@\s+|&nbsp;@'
    );
    $text = preg_replace($search, '', $document);
    return $text;
} www.jbxue.com
function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'http://astro.sina.com.cn/fate/astro_aries.html?prourl=0');//伪造来源
    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
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
foreach(array($constellation) as $astro){
    preg_match('/<span>(.*?)<em>(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)<div class="lotconts">(.*?)<\/div>/isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'), $matches);
 
file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], '<img'), substr_count($matches[5], '<img'), substr_count($matches[6], '<img'), substr_count($matches[7], '<img'), html2txt($matches[8]), html2txt($matches[9]), html2txt($matches[10]), html2txt($matches[11]), html2txt($matches[12]), $matches[13])));
unset($matches);
$array=json_decode(file_get_contents($astro),true);
}
 
//明天的日期
$mingtian=date('Ymd',strtotime('+1 day'));
foreach(array($constellation) as $astro){
    preg_match('/<span>(.*?)<em>(.*?)<\/em><\/span>.*?有效日期:(.*?)综合运势(.*?)爱情运势(.*?)工作状况(.*?)理财投资(.*?)健康指数(.*?)商谈指数(.*?)幸运颜色(.*?)幸运数字(.*?)速配星座(.*?)<div class="lotconts">(.*?)<\/div>/isu', curl('http://vip.astro.sina.com.cn/astro/view/'.$astro.'/day/'.$mingtian.''), $matches);
//这样正则,可以更小的受新浪改版所带来的影响

file_put_contents($astro, json_encode(array($matches[1], $matches[2], html2txt($matches[3]), substr_count($matches[4], '<img'), substr_count($matches[5], '<img'), substr_count($matches[6], '<img'), substr_count($matches[7], '<img'), html2txt($matches[8]), html2txt($matches[9]), html2txt($matches[10]), html2txt($matches[11]), html2txt($matches[12]), $matches[13])));
unset($matches);
$array2=json_decode(file_get_contents($astro),true);
}
 
//今天的运势
print_r($array)
//明天的运势
print_r($array2)
?>

您可能感兴趣的文章:
php实例之新浪星座运势
php中根据生日判断星座、生肖程序代码
php根据日期判断星座的函数代码
php使用身份证号码获取星座和生肖的例子
php根据生日计算年龄/生肖/星座实例
分享一个判断干支、生肖和星座的php函数
php根据出生日期计算年龄 生肖 星座程序
php使用新浪微博API开发用户授权功能
天天酷跑神秘天蝎怎么样 天天酷跑神秘天蝎值得入手吗
PHP:“草根语言”挑战“大腕”Java .Net

关键词: php 星座  php实例  星座   
[关闭]