Twitter status fails with advanced options

Edit: I answered my question. I do not know the correct etiquette to indicate this in the original question or simply by answering and accepting it myself.

How to add additional parameters for filtering retweets and responses?

I tried posting the question on twitter dev forums, but I think I will get better results and ask here.

I used the sample code from this answer to implement a working status search. I want to try to filter retweets and responses using the parameters that I saw on 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 error 23 "Failed to authenticate you."

My β€œassumption” is that I should not include additional parameters in the base url, but as additional parameters in the oauth array, where the sample code was commented.

 // 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 example 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>"; } ?> 
+7
source share
1 answer

So the change is pretty simple. Parameters do not apply to the main URL. The URL is just the base url, then the parameters are added to the oauth array, and the parameters are added to the url in the $ options array.


Shown with two parameters excludes retweets and responses.

 <?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>"; } ?> 
+3
source

All Articles