JSON:从数组中提取图像链接,抓取图像并在本地存储它们

时间:2013-12-29 22:33:16

标签: php json

我创建了这个函数,它每1000秒抓取一个外部(Instagram)JSON,在本地存储它并用它来显示一些信息。

<?php
function get_instagram($user_id=XXX,$count=80,$width=100,$height=100,$token="XXXXX"){
global $upload_dir;
    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$token.'&count='.$count;
    $cache = $upload_dir['basedir'].'/json/instagram/'.sha1($url).'.json';
    $i = 0;

    if(file_exists($cache) && filemtime($cache) > time() - 1000){
        $jsonData = json_decode(file_get_contents($cache), true);
    } else {
        $jsonData = json_decode(file_get_contents($url), true);
        file_put_contents($cache,json_encode($jsonData));
    }

    $result = '<div id="instagram">'.PHP_EOL;
    foreach ($jsonData['data'] as $value) {
        $title = (!empty($value['caption']['text']))?' '.$value['caption']['text']:'...';
        $location = (!empty($value['location']['name']))?' presso '.$value['location']['name']:null;
        $caption = '<div style="display: none;">'.htmlentities($title, ENT_QUOTES, "UTF-8").'<br><em style="font-size:11px">Scattata il '.htmlentities(strftime('%e %B %Y alle %R', $value['caption']['created_time'])).' '.htmlentities($location).' (<a target="_blank" style="color:darkgrey" rel="nofollow" href="http://maps.google.com/maps?q='.htmlentities($value['location']['latitude']).',+'.htmlentities($value['location']['longitude']).'">mappa</a>)</em></div>';

    if($i==6) {
    $result .= '<a style="display:none" class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"></a> '.$caption.PHP_EOL;;}
    else {
    $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value['images']['standard_resolution']['url'].'"><img id="thumb'.++$i.'" src="'.$value['images']['low_resolution']['url'].'" alt="'.$value['caption']['text'].'" width="'.$width.'" height="'.$height.'" /></a> '.$caption.PHP_EOL;}
    }
    $result .= '</div>'.PHP_EOL;
    return $result;
}
echo get_instagram();
?>

这是如何组成JSON的示例(仅一个条目):

{
   "pagination":{
      "next_url":"https:\/\/api.instagram.com\/v1\/users\/15203338\/media\/recent?access_token=15203338.3d61d31.705be3b7805a412ebd10d05196ea57cf\u0026count=1\u0026max_id=618767037278465762_15203338",
      "next_max_id":"618767037278465762_15203338"
   },
   "meta":{
      "code":200
   },
   "data":[
      {
         "attribution":null,
         "tags":[

         ],
         "type":"image",
         "location":{
            "latitude":41.897323268,
            "name":"Porta San Lorenzo",
            "longitude":12.511488861,
            "id":4798584
         },
         "comments":{
            "count":0,
            "data":[

            ]
         },
         "filter":"X-Pro II",
         "created_time":"1387982800",
         "link":"http:\/\/instagram.com\/p\/iWTVePtC7i\/",
         "likes":{
            "count":2,
            "data":[
               {
                  "username":"mozgyal",
                  "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_855320445_75sq_1388054204.jpg",
                  "id":"855320445",
                  "full_name":"mozgyal"
               },
               {
                  "username":"lidia_rz",
                  "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_283508926_75sq_1377609787.jpg",
                  "id":"283508926",
                  "full_name":"Lidiarizzi"
               }
            ]
         },
         "images":{
            "low_resolution":{
               "url":"http:\/\/distilleryimage7.s3.amazonaws.com\/592551106d7311e39b0b120baf64fd93_6.jpg",
               "width":306,
               "height":306
            },
            "thumbnail":{
               "url":"http:\/\/distilleryimage7.s3.amazonaws.com\/592551106d7311e39b0b120baf64fd93_5.jpg",
               "width":150,
               "height":150
            },
            "standard_resolution":{
               "url":"http:\/\/distilleryimage7.s3.amazonaws.com\/592551106d7311e39b0b120baf64fd93_8.jpg",
               "width":640,
               "height":640
            }
         },
         "users_in_photo":[

         ],
         "caption":{
            "created_time":"1387982800",
            "text":"Pappa",
            "from":{
               "username":"multiformeingegno",
               "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_15203338_75sq_1323624047.jpg",
               "id":"15203338",
               "full_name":"Lorenzo"
            },
            "id":"618767037731450618"
         },
         "user_has_liked":false,
         "id":"618767037278465762_15203338",
         "user":{
            "username":"multiformeingegno",
            "website":"",
            "profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_15203338_75sq_1323624047.jpg",
            "full_name":"Lorenzo",
            "bio":"",
            "id":"15203338"
         }
      }
   ]
}

如何获取图像(例如standard_resolution和low_resolution),将它们存储在本地并使用它们而不是远程图像?

1 个答案:

答案 0 :(得分:0)

鉴于您提供的json数据是一个条目。

//foreach entry as $jsonData

$ret = json_decode($jsonData, true);

$ret = $ret['data'];
$ret = $ret['images'];

$low_res = $ret['low_resolution'];
$stand_res = $ret['standard_resolution'];

//do some database stuff to save it.

//on to the next one

//end foreach

尚未测试,所以希望它有效: - )