HTTPS站引用腾讯公益404

腾讯公益404 并不支持 HTTPS 站点的引用已经支持HTTPS,直接拉 https://qzone.qq.com/gy/404/data.js,以下为原解决方案:

为了公益事业研究了下,发现只要拉到 http://qzone.qq.com/gy/404/data.js 数据即可,但 HTTPS 站是不能直接拉非安全协议内容,所以还要在自己网站上用一个 PHP 做中转,如下:

<?php
header('Content-type: text/javascript');
$filename = 'data.js';
clearstatcache();
$lastTime = filemtime($filename);
$nowTime = time();
if ($nowTime - $lastTime > 604800) {
    $url = 'http://qzone.qq.com/gy/404/data.js';
    $html = file_get_contents($url);
    echo $html;
    $file = fopen($filename, "w");
    fwrite($file, $html);
    fclose($file);
} else {
    readfile($filename);
}

然后就是,用 JS 读数据显示了,不多说,我的代码如下:

jQuery.getScript("https://qzone.qq.com/gy/404/data.js", function () {
    var max = jsondata.data.length;
    var num = Math.round(Math.random() * max);
    var picUrl = jsondata.data[num].child_pic;
    var moreUrl = jsondata.data[num].url;
    var more = ' <a href="' + moreUrl + '" target="_blank">查看详情</a>';
    var name = '<strong>' + jsondata.data[num].name + '</strong>';
    var sex = '(' + jsondata.data[num].sex + ")" + ',';
    var birthTime = '出生日期:' + jsondata.data[num].birth_time + ',';
    var lostTime = '失踪时间:' + jsondata.data[num].lost_time + ',';
    var lostPlace = '失踪地点:' + jsondata.data[num].lost_place + ',';
    var childFeature = '失踪人特征描述:' + jsondata.data[num].child_feature;

    $('h2').text("你访问的页面找不回来了,但是我们可以一起寻找失踪宝贝");
    $('.img-responsive').attr("src", picUrl);
    $('p.text-muted').addClass('text-left').html(name + sex + birthTime + lostTime + lostPlace + childFeature + more);
});

效果:https://s5s5.me/gongyi404

发布者:s5s5

https://s5s5.me