dtufl26404 2013-08-01 08:03
浏览 34
已采纳

获取Guzzle Twitter响应数据

Hopefully the solution to this is a lot more simple than what I've been trying!

I've got a Symfony 2.3 app where I'm attempting to get the number of followers of a Twitter users account. I get back data but I can't access it by array key/index values.

Controller Action:

public function twitterAction(){
    $twitterClient = $this->container->get('guzzle.twitter.client');
    $response = $twitterClient->get('1.1/followers/ids.json?screen_name=ACCOUNTNAME')
            ->send()->json();

    return $this->render('CatablogSiteBundle:Default:status.html.php', array('response' => $response));

}

View:

<?php
var_dump($response);
echo '<br><br>';
echo gettype($response);
echo '<br><br>';
echo $response[0];
?>

I get back data that I want to use from var_dump, gettype responds with type Array , and attempting to reference $response[0] will fail completely.

What can I do to access data inside the response Object?

edit:

Of course I can't echo $response[0] ... (wrong type) don't try and code tired guys. Solved whilst going over NHG's answer, which is still helpful for anyone having problems with Guzzle.

  • 写回答

1条回答 默认 最新

  • duanhuo0577 2013-08-01 10:02
    关注

    If I understood TwitterClient extends Guzzle\Service\Client (based on https://github.com/RobinvdVleuten/guzzle-clients/blob/master/Twitter/TwitterClient.php, isn't it?). So, let's try this one:

    echo $response->getBody();
    // for getting body
    echo $response->getHeader('Content-Length');
    // for getting Content-Length body
    $data = $response->json();
    // for getting response in json format
    

    Docs: http://guzzlephp.org/tour/http.html#using-a-client-object

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?