php - Random image generator from Imgur album -


i'm looking create apache site using php each time person directs it, grab random image created imgur album full of images , display image onload without ever leaving website. thinking need use imgur api make sort of thing php don't know start. know how may able this?

i made javascript alternative using arrays fetches images listed in javascript code, make use of imgur api , automatically fetch images' urls imgur album , randomly pick 1 display every time user loads webpage.

<!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> 

this json returned album using imgur api, can transform json array within php , use array_rand() select random one:

{     "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 } 

more information in imgur api documentation

being $json string coming imgur servers, code gather random link:

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

Comments