9

Edit: I answered my own question. I do not know the proper etiquette for indicating this in the original question or just answering and accepting it myself.

How do I add additional parameters to filter out retweets and replies?

I tried submitting a question to the twitter dev forums but I think I will get better results also asking it here.

I have used sample code from this answer to implement a working retrieval of statuses. I want to try and filter out the retweets and replies by using the parameters I saw on the twitter dev api and update the url from

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

to

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false";

which returns an error 23 "Could not authenticate you".

My "guess" is that I shouldn't be including the additional parameters in the base url, but as additional parameters into the oauth array where the sample code was commented out.

// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
    //CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_HEADER => false,
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false);

Full Sample code with removed tokens.

<?php

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}


function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;
}


//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?include_rts=false";

$oauth_access_token = "removed";
$oauth_access_token_secret = "removed";
$consumer_key = "removed";
$consumer_secret = "removed";


$oauth = array( 'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0');


$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;


// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);


$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);


$twitter_data = json_decode($json);

foreach ($twitter_data as $tweet)
{
    $text = $tweet->text;
    echo $text . "</br></br>";
}

?>
Community
  • 1
  • 1
TEEKAY
  • 1,156
  • 1
  • 10
  • 25
  • 3
    Twitter says the request is a `GET` request, so putting the parameters in the `POST` array will probably not give the desired result. – David Titarenco Oct 20 '12 at 17:53
  • I agree. I was drawing at straws with that. – TEEKAY Oct 22 '12 at 13:02
  • Right now I am resigned to filter them out programatically after the fact, but saw this post on twitter dev forums and it seems relevant. Same code sample, different direction but similar problem. I'll probably investigate it later. https://dev.twitter.com/discussions/12242 – TEEKAY Nov 02 '12 at 15:37
  • 1
    I've had the same problem using TwitterOauth on github, I think it's broken at Twitter and they have not admitted it yet – Catharsis Dec 19 '12 at 17:17
  • @Catharsis thanks for the reply. The related question I was looking at had someone comment similarly, and someone has posted an answer, though I haven't been able to investigate it yet. See the answer by lackovic10 that references a comment on the answer posted by Rivers. http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1 – TEEKAY Dec 27 '12 at 15:35

1 Answers1

4

So the change is pretty simple. The params don't belong in the main url. The url is just the base url, then the params are added to the oauth array, and the params are additionally added to the url in the $options array.


Shown with two parameters exclude retweets and replies.

<?php

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}


function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;
}


$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";

$oauth_access_token = "removed";
$oauth_access_token_secret = "removed";
$consumer_key = "removed";
$consumer_secret = "removed";


$oauth = array( 'exclude_replies' => 'true',
                'include_rts' => 'false',
                'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0');


$base_info = buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;


// Make Requests
$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url . '?exclude_replies=true&include_rts=false',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);


$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);


$twitter_data = json_decode($json);

foreach ($twitter_data as $tweet)
{
    $text = $tweet->text;
    echo $text . "</br></br>";
}

?>
TEEKAY
  • 1,156
  • 1
  • 10
  • 25