douyi1779 2013-08-01 04:28
浏览 38
已采纳

获取,解码和重新编码JSON

I am attempting to fetch JSON from Instagram according to a number of URL parameters, the JSON is then decoded and then the objects required are then encoded into my own JSON format. Whilst I know this sound a little ridiculous, it is what is required. My only issue here is that for some reason it does not encode each section of JSON, it will only work for one item. The code is as below.

<?php

function instagram($count=16){

$igtoken = $_GET['igtoken'];
$hashtag = $_GET['hashtag'];


    $url = 'https://api.instagram.com/v1/tags/'.$hashtag.'/media/recent/?access_token='.$igtoken.'&count='.$count;

        $jsonData = json_decode((file_get_contents($url)));

$jsonData = json_decode((file_get_contents($url)));
    foreach ($jsonData->data as $key=>$value) {

            $response = array();
            $response["data"] = array();
            $data = array();
            $data["createdtime"] = $value->caption->created_time;
            $data["username"] = $value->caption->from->username;
            $data["profileimage"] = $value->caption->from->profile_picture;
            $data["caption"] = $value->caption->text;
            $data["postimage"] = $value->images->standard_resolution->url;

            array_push($response["data"], $data);
            $result = json_encode($response); 

    }

    return $result;
}

echo instagram();
?>

It will work for each section of JSON if I do something like this instead:

    $result .= '<li> 
                      '.$value->caption->from->username.'<br/>
                      '.$value->caption->from->profile_picture.'<br/>
                      '.$value->caption->text.'<br/>
                      '.$value->images->standard_resolution->url.'<br/>
                      '.$value->caption->created_time.'<br/>
                      </li>';

I feel I have bodged up somewhere with the array, however i'm not entirely sure.

  • 写回答

1条回答 默认 最新

  • douduidui1046 2013-08-01 04:42
    关注

    What if we move $response["data"] and $result varia out of foreach?

    Have you tried this?

    $response = array();
    $response["data"] = array();
    foreach ($jsonData->data as $key=>$value) {            
    
            $data = array();
            $data["createdtime"] = $value->caption->created_time;
            $data["username"] = $value->caption->from->username;
            $data["profileimage"] = $value->caption->from->profile_picture;
            $data["caption"] = $value->caption->text;
            $data["postimage"] = $value->images->standard_resolution->url;
    
            array_push($response["data"], $data);
    }
    $result = json_encode($response); 
    return $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件