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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图