Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top