views:

22

answers:

2

I am trying to perform a Twitter search using the PEAR package Services_Twitter. Unfortunately this only returns an array of status ids, for example *(var_dump)*:

object(stdClass)#88 (2) {
  ["statuses"]=>
  array(11) {
    [0]=>
    int(49497593539)
    [1]=>
    int(49497593851)
    [2]=>
    int(49497598001)
    [3]=>
    int(49497599055)
    [4]=>
    int(49497599597)
    [5]=>
    int(49497600733)
    [6]=>
    int(49497602607)
    [7]=>
    int(49497607031)
    [8]=>
    int(49497607453)
    [9]=>
    int(49497609577)
    [10]=>
    int(49497610605)
  }
  ["created_in"]=>
  float(0.008847)
}

The script I'm using is similar to this test script I wrote:

<?php
//$oAuth = new HTTP_OAuth_Consumer( /** Supply oAuth details here **/ );
$Twitter = new Services_Twitter();
//$Twitter->setOAuth($oAuth);
try {
    $Response = $Twitter->search(array(
      "q"   => "#FF -RT OR #FollowFriday -RT",
      "rpp" => 10,
      "since_id"  => 23982086000,
      "result_type" => "recent"
    ));
    var_dump($Response);
  } catch (Exception $e) {
    fwrite(STDERR, $e->getMessage());
  }
?>

Since I want to scan the tweets for certain words and want to know when it was posted and by whom, I would need to request all these statuses one by one. But according to the example response in the Twitter API documentation they already return all the necessary information about the tweets (which is kinda obvious).

So, the question is: How can I access this information using Services_Twitter?

Kind Regards,

Arno

A: 

Are you using a custom Services_Twitter, I just did a search through the class via Pear Documentation and was unable to find the search function. However, it seems like most of the returns for that class is a simple_xml object. Given that I would look through the documentation there and see how you can pull that data out. It would also help looking at how Twitter returns the response in XML format.

Brad F Jacobs
It's wrapped with __call()!
Till
Ah, thanks for clearing that up!
Brad F Jacobs
Btw, the default response is JSON.
Till
From the Twitter API, but the Services_Twitter returns simple_xml element. From the Doc for __call() `Return: of SimpleXMLElement`
Brad F Jacobs
`Services_Twitter::$options` says JSON it is. The docs need some updates! :)
Till
Gotcha :P Well cannot help bad documentation.
Brad F Jacobs
+2  A: 

So as I said ->search() is wrapped through Services_Twitter::__call().

But here's the mis-understanding!

Two searches:

This is confusing as search.twitter.com returns the results as you'd expect them and the other API method just the status IDs.

For some reason only when you search for trends search.twitter.com is used. Otherwise it's the API methods. If you want to help, please open a ticket on PEAR and I can try to implement this for you.

A quickfix for you is this script:

<?php
$uri  = 'http://search.twitter.com/search.json?';
$uri .= http_build_query(
    array(
        "q"           => "#FF -RT OR #FollowFriday -RT",
        "rpp"         => 10,
        "since_id"    => 23982086000,
        "result_type" => "recent"
));

$response = file_get_contents($uri);
if ($response === false) {
    fwrite(STDERR, "Could not fetch search result.");
    exit(1);
}

$data = json_decode($response);
var_dump($data);
Till
I will adjust my ticket on the PEAR site as soon as possible. I wanted to use oAuth, to perform more searches/hour (since oauth calls have a higher rate-limit). Correct me if I'm wrong, it might be different for the Search API ofcourse.
Arno Moonen
No, no - you're probably correct. I wonder if the oauth'd API supports what you want though. From what I see, by looking at the straight request, the API only returns those user IDs. So in turn, you'd need to look up the full message - one by one.
Till