简体   繁体   中英

Merge Instagram API json file with my own, in PHP

I am looking to extract specific limited data from Instagram's json file, and encode it to my own ever-growing json file, merging where duplicate id 's are present, or just adding to it otherwise. I know how to echo the fields I am interested in, but not how to format and encode the json the way I want it, nor how to merge.

<?php 
  foreach($instagram_array['data'] as $image){

    $url = $image['images']['standard_resolution']['url'];
    $date_shot = date('M d, Y', $image['created_time']);
    $likes = $image['likes']['count'];
    $id = $image['id'];

      echo $date_shot;
      echo $likes;
      echo $url;
      $echo $id;

  }
?>

I would be pretty happy with json that looked like this:

{
"url":"http://instagram.com/12345.jpg"
"id":"35228330387",
"created_time":"1424497894",
"likes":"20"
}

but have no idea how to reformat it. I did have some success finishing off my encoding and merging BEFORE I decided I wanted to get rid of all the chuff... like so:

$user_array = array_merge_recursive($instagram_array, $user_array);
file_put_contents($user_id . '.json', json_encode($user_array, JSON_FORCE_OBJECT));

but it got out of hand pretty quick with just 100 images...

ps - I have not pasted any of the json file here, as it's a beast and I am hoping the foreach loop has the info needed. But in case not, here is a link :

Your question is not clear. I assume you know how to get the values for the variables url,id,created_time and likes. Just put them into an array and json encode the array.

$instaInfo = array();
$url = $image['images']['standard_resolution']['url'];
$date_shot = date('M d, Y', $image['created_time']);
$likes = $image['likes']['count'];
$id = $image['id'];

$instaInfo["url"] = $url;
$instaInfo["id"] = $id;
$instaInfo["created_time"] = $date_shot;
$instaInfo["likes"] = $likes;

$json_you_want = json_encode($instaInfo);

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM