简体   繁体   English

用PHP自己合并Instagram API json文件

[英]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. 我希望从Instagram的json文件中提取特定的有限数据,并将其编码为我自己不断增长的json文件,合并存在重复id的位置,否则只是添加它。 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. 我知道如何回应我感兴趣的字段,但不知道如何按照我想要的方式格式化和编码json,也不知道如何合并。

<?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: 我会很高兴看起来像这样的json:

{
"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: 我确实取得了一些成功,完成了我的编码和合并之前,我决定要摆脱所有的chuff ......就像这样:

$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... 但只有100张图片,它很快就失控了......

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. ps - 我没有粘贴任何json文件,因为它是一个野兽,我希望foreach循环有所需的信息。 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. 我假设您知道如何获取变量url,id,created_time和like的值。 Just put them into an array and json encode the array. 只需将它们放入数组中,json就可以对数组进行编码。

$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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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