【问题标题】:Random image generator from Imgur album来自 Imgur 专辑的随机图像生成器
【发布时间】:2016-05-01 01:43:23
【问题描述】:

我希望使用 PHP 创建一个 Apache 站点,这样每次有人指向它时,它都会从先前创建的 Imgur 相册中抓取一张随机图像,其中包含图像,并在不离开网站的情况下显示加载的图像。我想我需要使用 Imgur API 来用 php 做这种事情,但我不知道从哪里开始。有谁知道我怎么能做到这一点?

我使用数组制作了这个 javascript 替代方案,但这仅获取 javascript 代码中列出的图像,我想利用 Imgur API 并自动从 Imgur 相册中获取所有图像的 URL 并随机选择一个显示每个用户加载网页的时间。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Random</title>

<script type="text/javascript">
function randomlinks(){
    var myrandom=Math.round(Math.random()*263)
    var links=new Array()
links[0]="http://i.imgur.com/blablablabla.jpg"


    window.location=links[myrandom]

}
</script>

  </head>
  <body onload="randomlinks()">

  </body>
</html>

【问题讨论】:

    标签: php random generator imgur


    【解决方案1】:

    这是使用imgur api从相册返回的json,您可以在PHP中将此json转换为数组,然后使用array_rand()随机选择一个:

    {
        "data": {
            "id": "lDRB2",
            "title": "Imgur Office",
            "description": null,
            "datetime": 1357856292,
            "cover": "24nLu",
            "account_url": "Alan",
            "account_id": 4,
            "privacy": "public",
            "layout": "blog",
            "views": 13780,
            "link": "http://alanbox.imgur.com/a/lDRB2",
            "images_count": 11,
            "images": [
                {
                    "id": "24nLu",
                    "title": null,
                    "description": null,
                    "datetime": 1357856352,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 855658,
                    "views": 135772,
                    "bandwidth": 116174397976,
                    "link": "http://i.imgur.com/24nLu.jpg"
                },
                {
                    "id": "Ziz25",
                    "title": null,
                    "description": null,
                    "datetime": 1357856394,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 919391,
                    "views": 135493,
                    "bandwidth": 124571044763,
                    "link": "http://i.imgur.com/Ziz25.jpg"
                },
                {
                    "id": "9tzW6",
                    "title": null,
                    "description": null,
                    "datetime": 1357856385,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 655028,
                    "views": 135063,
                    "bandwidth": 88470046764,
                    "link": "http://i.imgur.com/9tzW6.jpg"
                },
                {
                    "id": "dFg5u",
                    "title": null,
                    "description": null,
                    "datetime": 1357856378,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 812738,
                    "views": 134704,
                    "bandwidth": 109479059552,
                    "link": "http://i.imgur.com/dFg5u.jpg"
                },
                {
                    "id": "oknLx",
                    "title": null,
                    "description": null,
                    "datetime": 1357856338,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 1749,
                    "height": 2332,
                    "size": 717324,
                    "views": 32938,
                    "bandwidth": 23627217912,
                    "link": "http://i.imgur.com/oknLx.jpg"
                },
                {
                    "id": "OL6tC",
                    "title": null,
                    "description": null,
                    "datetime": 1357856321,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 1443262,
                    "views": 32346,
                    "bandwidth": 46683752652,
                    "link": "http://i.imgur.com/OL6tC.jpg"
                },
                {
                    "id": "cJ9cm",
                    "title": null,
                    "description": null,
                    "datetime": 1357856330,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 544702,
                    "views": 31829,
                    "bandwidth": 17337319958,
                    "link": "http://i.imgur.com/cJ9cm.jpg"
                },
                {
                    "id": "7BtPN",
                    "title": null,
                    "description": null,
                    "datetime": 1357856369,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 844863,
                    "views": 31257,
                    "bandwidth": 26407882791,
                    "link": "http://i.imgur.com/7BtPN.jpg"
                },
                {
                    "id": "42ib8",
                    "title": null,
                    "description": null,
                    "datetime": 1357856424,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 2592,
                    "height": 1944,
                    "size": 905073,
                    "views": 30945,
                    "bandwidth": 28007483985,
                    "link": "http://i.imgur.com/42ib8.jpg"
                },
                {
                    "id": "BbwIx",
                    "title": null,
                    "description": null,
                    "datetime": 1357856360,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 1749,
                    "height": 2332,
                    "size": 662413,
                    "views": 30107,
                    "bandwidth": 19943268191,
                    "link": "http://i.imgur.com/BbwIx.jpg"
                },
                {
                    "id": "x7b91",
                    "title": null,
                    "description": null,
                    "datetime": 1357856406,
                    "type": "image/jpeg",
                    "animated": false,
                    "width": 1944,
                    "height": 2592,
                    "size": 618567,
                    "views": 29259,
                    "bandwidth": 18098651853,
                    "link": "http://i.imgur.com/x7b91.jpg"
                }
            ]
        },
        "success": true,
        "status": 200
    }
    

    更多信息在IMGUR API Documentation

    作为 $json 来自 imgur 服务器的字符串,此代码将收集一个随机链接:

    $array=json_decode($json);
    
    $links = array();
    
    foreach($array->data->images as $img)
    $links[]=$img->link;
    
    echo $links[array_rand($links)];
    

    【讨论】:

    • 如何将其解析为 PHP? json_decode() 也许?
    • 准确地说,json_decode 从 json 字符串中创建了一个数组。
    • 从那以后我有点迷路了。我知道我必须从数组中获取某个值,但我自己对 JSON 并不熟悉。
    • 这样的东西有帮助吗? $jsonArr = json_decode($str, TRUE); $rand_array_key = array_rand($jsonArr); echo $jsonArr[$rand_array_key]['id'];
    • 非常感谢,所以 $json 将是来自 imgur api 服务器的 url 字符串?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2011-05-29
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多