如何从pinterest oauth api php回显数组数据

时间:2016-04-04 16:00:00

标签: php oauth pinterest

我正在尝试使用oauth api从pinterest获取用户个人资料。 用户数据的代码:

$me = $pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[large]'
));

和echo结果:

echo $me;

输出如下:

{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}

现在我希望将此结果作为

回显
id="195414208739840616"
username="rajivsharma033"
first_name="Rajiv"

依旧...... 请帮帮我。

2 个答案:

答案 0 :(得分:1)

由于您获得json数据,因此您需要使用json_decode(): -

   <?php
    $me = '{"id":"195414208739840616","username":"rajivsharma033","first_name":"Rajiv","last_name":"Sharma","bio":null,"created_at":null,"counts":null,"image":{"large":{"url":"https:\/\/s-media-cache-ak0.pinimg.com\/avatars\/rajivsharma033_1459712414_280.jpg","width":280,"height":280}}}';

    $array_data = json_decode($me); 
    echo "<pre/>";print_r($array_data);

    foreach ($array_data as $key=>$value){

        if($key == 'image'){
            echo $key. " url is=" . $value->large->url .'<br/>';
        }else{

            echo $key. "=" . $value .'<br/>';
        }
    }

答案 1 :(得分:0)

我会用两次foreach来解决它:

$a = json_decode($me);
 foreach ($a as $key=>$value){
echo $key.'="'.$value.'"<br/>';
}
foreach ($a['image']['large'] as $key=>value){
 echo 'image-large-'$key.'="'.$value.'"<br/>';
}

或者,您可以递归执行此操作:

function echojson($string=''){
 $a = json_decode($me);
 foreach ($a as $key=>$value){
  if (is_array($value)) echojson($string.'-'.$key);
  else
  echo $string.$key.'="'.$value.'"<br/>';
 }
}