Giter Site home page Giter Site logo

twitter-api-php's Introduction

twitter-api-php

Simple PHP Wrapper for Twitter API v1.1 calls

Total Downloads Build Status Version

Changelog || Examples || Wiki

Instructions in StackOverflow post here with examples. This post shows you how to get your tokens and more. If you found it useful, please upvote / leave a comment! :)

The aim of this class is simple. You need to:

You really can't get much simpler than that. The above bullet points are an example of how to use the class for a POST request to block a user, and at the bottom is an example of a GET request.

Installation

Normally: If you don't use composer, don't worry - just include TwitterAPIExchange.php in your application.

require_once('TwitterAPIExchange.php');

Via Composer:

composer require j7mbo/twitter-api-php

How To Use

Set access tokens

$settings = array(
    'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
    'consumer_key' => "YOUR_CONSUMER_KEY",
    'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

Choose URL and Request Method

$url = 'https://api.twitter.com/1.1/blocks/create.json';
$requestMethod = 'POST';

Choose POST fields (or PUT fields if you're using PUT)

$postfields = array(
    'screen_name' => 'usernameToBlock', 
    'skip_status' => '1'
);

Perform the request!

$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
    ->setPostfields($postfields)
    ->performRequest();

GET Request Example

Set the GET field BEFORE calling buildOauth(); and everything else is the same:

$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

That is it! Really simple, works great with the 1.1 API. Thanks to @lackovic10 and @rivers on SO!

twitter-api-php's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twitter-api-php's Issues

Using hashtags in Get field

Hi, thanks for this wrapper, works well
But, i have problem with using hashtags in Get field

this works fine
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=bratislava&count=3';

but this not
$getfield = '?q=%23bratislava&count=3';
it returns {"message":"Could not authenticate you","code":32}

Where I made mistake? Do you have some recommendation for me please?
Thank you

Critical? Missing rawurlencode

From twitter's API docs on signatures under "Collecting Parameters":

1. Percent encode every key and value that will be signed.

As of now, only the key is being encoded when building the base string, as seen here. I had to add rawurlencode to the value before it started working.

For those on the same hunt as me, I also found this a very useful thread.

Count does not work

Hi there. I'm using this API to fetch the latest tweets with the hashtag mitthverfi on a website. My website only displays the 4 latest tweets and when I set my getfield to count=10 or some higher number that does not change anything. When I try count=1 I only get one tweet.

How can I make this display all tweets or maby just more then the latest 4 ?

Release tags

Would be really helpful if release tags were introduced (+ obviously updating composer.json version accordingly) so that this library can be safely used in production.

empty getfield causes error 32

Hi,

when doing an api get request where no parameters are needed (i.e. geo/id/:place_id), the api responds with error 32 (Could not authenticate you), if you set an empty getfield.

Example:

$url = "https://api.twitter.com/1.1/geo/id/df51dec6f4ee2b2c.json";
$getfield = "";

A fast fix would be to set

$getfield = "?foo=bar";

the api will just ignore that parameter and the oauth works.

Maybe this is helpful too:
https://dev.twitter.com/discussions/12242

Could not authenticate you, code 32

I believe I have a pretty simple usage of this library, but I'm receiving the following response from Twitter:

{"errors":[{"message":"Could not authenticate you","code":32}]}

Here is my code;

require_once 'TwitterAPIExchance.php';
$settings = array(
    'oauth_access_token' => "<snip>",
    'oauth_access_token_secret' => "<snip>",
    'consumer_key' => "<snip>",
    'consumer_secret' => "<snip>"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfields = '?q=foobar&result_type=recent&count=50';
$twitter = new TwitterAPIExchange($settings);

// Get tweets
$data = $twitter->buildOauth($url, $requestMethod)
            ->setGetfield($getfields)
            ->performRequest();

print $data;

I started off using an old Twitter app used for something else so I create an entirely new app and switched to those credentials instead. Still no dice.

I'm working on a dev area so moved the script to the production server to make sure it wasn't that. It wasn't.

Any ideas what, if anything, I'm doing wrong?

New version returns null, older version works

Hi @J7mbo

Firstly, thanks for the very useful twitter-api-php.

I noticed that the most recent version of twitter-api-php returns null for me, while an older version of twitter-api-php works great. Here are the differences between the two: http://www.diffchecker.com/g6jnkzfp . I used WAMP Server v2.4 and the script from this reply (#21) to test this.

I discovered this when I found it very strange that an application I wrote a couple of months ago works great with the older twitter-api-php, and a new application I'm writing now using the newer twitter-api-php returns null. I tried all of the recommendations suggested in this reply (#21) including generating new access keys but so far, the only thing that works for me is to use the older version of twitter-api-php.

Any recommendations? Thanks again.

Windows XP:
WAMP Server v2.4
PHP 5.4.16
Apache 2.4.4

error 32 if get parameters are not passed

Hey,

Thanks for the library.

I was trying to call "https://api.twitter.com/1.1/statuses/home_timeline.json" API using your library and was getting error 32 from twitter.

the code looks like

$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$getfield = '';
$requestMethod = 'GET';

echo $twitterLib->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

After some research I found some issues in the TwitterAPIExchange file at line 152. It was just checking for is_null and since I had given this condition ( $getfield = ''; ) it was failing and going inside the loop.

So you can just fix it with [ if (!is_null($getfield) && trim($getfield)!="") ] so it checks even if the values are empty.

Regards

New tokens

Is it possible to use this library to identify users other than myself? How do I go about requesting authorisation and posting as someone who authorised?

Thanks,
Cris

oauth_nonce?

line 143 you set oauth_nonce to time(). Doesn't that variable need to be a 32 character string? I keep getting "Could not authenticate you","code":32 responses from your api.

Thanks.

Creating a search query for hashtag or posts created by a user?

I'm trying to figure out if there's a way to query tweets for all those that include a hashtag or have been created by a specific user with your library. I absolutely love what you've done, but wanted to see if this is possible. Right now when I setup my request url like '?q=#hashtag OR from:username' it is just returning all the tweets from the Twitter account linked to the API credentials I'm using.

Any thoughts on all that?

Cannot get to work on Localhost

Hi,

I would 1st like to thank you for this class... its bloody awesome!

I have WAMP installed locally for development which I can't seem to get this to work on. I tried adding http://localhost:8080/ to the allowed domains but it did not work.

Any ideas?

Installs as .git repo

After I run php composer.phar update it installs as a git versioned directory. When I git add . and then do a git commit followed by a push, then, when I'm on another machine and do a git pull the files don't come in and my production app is now broken. Thank you so much for writing this library. Do you know how I would go about resolving my issue? Why does php composer.phar update install a new git repo? I'd like just the files. What are your thoughts? Thank you!!

Bad Creds are Good Creds

I tried using this and was really hoping it would work but of course I ran into a wall. I plugged in my creds from my Twitter Application and I get

'Make sure you are passing in the correct parameters'

I have verified that the parameters are correct by testing them on another code snippet I found for the Twitter API and it actually worked fine and I got all of my tweets. I'd like to use yours as it is much cleaner. If you have any information I'd appreciate it. Thanks for the code though.

blank page response when calling https://api.twitter.com/1.1/statuses/user_timeline.json

Hi,

I am trying to get a list of tweets from my own personal twitter timeline so that i can display them on my own website. I have installed your code and have tried to use it in the exact way that you set out, but when i use it all im getting is a blank page (nothing is being displayed at all). libcurl/7.16.0 OpenSSL/0.9.8g zlib/1.2.3 is enabled and running on my server. My code is as follows (keys/username are blanked out for obvious reasons):-

[CODE]

"KEY", 'oauth_access_token_secret' => "KEY", 'consumer_key' => "KEY", 'consumer_secret' => "KEY" ); /*\* URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/ $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $requestMethod = 'GET'; /*\* GET fields required by the URL above. See relevant docs as above **/ $getfield = '?user_id=MyUserID&count=3'; /*\* Note: Set the GET field BEFORE calling buildOauth(); **/ $twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); var_dump(json_decode($response)); ?>

[/CODE]

error using POST method

Doing this:
$arguments = ?user_id=158812437%2C114077134%2C572846119
$host = https://api.twitter.com/1.1/users/lookup.json
$requestMethod = 'POST'

json = $twitter->setGetfield($arguments)
->buildOauth($host, $requestMethod)
->performRequest();

twitter returns an error 32, could not authenticate you
changing requestMethod to GET and without touching anything else, works

i think the problem is because of the
Content-Type: application/x-www-form-urlencoded
after migrating to api 1.1

Problems with special characters like á, é, í, ó, ú and ñ

I'm using this API and works excellent. But I have some problems using spanish characters in my status updates. When I use some of those characters they are converted in a weird code in twitter, for example, let's take this message:

"Inameh anuncia más lluvia para este marte" the "á" looks like this in twitter:

twitter-api-issue

Part of the API output is the follow:

{"created_at":"Tue Sep 03 15:26:30 +0000 2013",..........,"text":"Inameh anuncia m\ufffds lluvia para este martes http://t.co/G2jIn0VabK",

As it can see the "más" is changed by "m\ufffds".

So far I have tried using: htmlentities, utf8_encode, utf8_decode, htmlspecialchars and nothing works.

Can anyone help me with this?

Thanks and regards from Venezuela.

Get third-party tweets?

I'm trying to use this library to replace a feature on a friend's website to present their latest tweet. I've created an application with Twitter, generated the right keys, and used your template files that have been modified via the 1.1 API documentation.

However, even though I specify my friend's twitter username, I get my tweets back from the API. Is this because I registered the application? (i.e. do I need to have my friend set up his own application) Or am I using the API wrong? I'm building the call as follows:

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?username=FRIEND_USERNAME&count=1&exclude_replies=true';
$requestMethod = 'GET';
$latest = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

Sort by encoded key

In your buildBaseString() function at Line 232 you're sorting the parameters before encoding their keys. If you look at step 2 on Twitter's Creating a signature page you'll see that you should actually:

Sort the list of parameters alphabetically[1] by encoded key[2].

You need to urlencode the keys before sorting. This will almost never create a problem if you sort before encoding but the potential exists and the order should be fixed.

Twitter API error with `users/lookup` endpoint when POST'ing, but not GET'ing.

I've been exploring the Twitter API, but when I tried using the 'users/lookup' endpoint, I consistently received "Sorry, that page does not exist" errors.

However, when using their dev console, I am able to get the correct result. This test code fails:

$url = 'https://api.twitter.com/1.1/users/lookup.json';
$fields = array('screen_name' => 'lifehacker');
$data = $twitter->buildOauth($url, 'POST')->setPostfields($fields)->performRequest();

All POST request seems to work without issue, except ones to users/lookup. I thought maybe the problem was that cURL was using multipart/form-data while the Twitter console was always sending requests as application/x-www-form-urlencoded, but I was unable to get anything except the "Sorry, that page does not exist" error code 34 regardless of the POST's request Content-Type I sent.

Frustratingly, I can't seem to figure out how to examine the request body using PHP cURL, only its headers.

When I change this to a GET, it works. This test code succeeds:

$url = 'https://api.twitter.com/1.1/users/lookup.json';
$fields = '?screen_name=lifehacker';
$data = $twitter->setGetfield($fields)->buildOauth($url, 'GET')->performRequest();

However Twitter's documentation explicitly mentions a POST is recommended for this endpoint:

You are strongly encouraged to use a POST for larger (up to 100 screen names) requests.

So, I feel like this is a bug.

nothing is printed

I am out of shape. I have not been coding for a few years. I set the setting values with my keys. I put a username of my choice. I commented the post command and tried only the get one. However, nothing is displayed. I am not sure what it's wrong. I set the E_ALL in php.ini to show error but nothing is shown at all. Any ideas?

here is my code:

'79029053####', 'oauth_access_token_secret' => 'f42ur3s####, 'consumer_key' => 'fJiOZy9####', 'consumer_secret' => 'XmLR4###' ); /*\* URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/ $url = 'https://api.twitter.com/1.1/blocks/create.json'; $requestMethod = 'POST'; /*\* POST fields required by the URL above. See relevant docs as above **/ $postfields = array( 'screen_name' => 'usernameToBlock', 'skip_status' => '1' ); /*\* Perform a POST request and echo the response **/ /* $twitter = new TwitterAPIExchange($settings); echo $twitter->buildOauth($url, $requestMethod) ->setPostfields($postfields) ->performRequest(); */ /** Perform a GET request and echo the response **/ /** Note: Set the GET field BEFORE calling buildOauth(); **/ $url = 'https://api.twitter.com/1.1/followers/ids.json'; $getfield = '?screen_name=usernamehere'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest();

Application-only auth

There are a few API calls that don't require a logged-in user to access, just an application key/secret. Are there any plans to implement this here? At the moment, both application and OAuth keys are required for any request (or Exception: Make sure you are passing in the correct parameters if not).

Render and display result

Hi J7mbo,

Sorry, not really a issue...

thanks for your code! Works great.
But i get as a result of the call an array of data from twitter.
(I use:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=Tele2partner&count=3';

I need only the message and the date (time ago) in a loop.

How can I control and style /display the result?
Can you give me an example?

Thanks!
E

Call to user_timeline from cron results in blank response

Hi,

I'm having a peculiar problem where my script makes a GET request to statuses/user_timeline via cron and the response is blank.

I've tested the request with the Twitter OAuth tool and run a self-executed test script using the same routine, both of which return the expected response. So why might calling the script from cron result in a blank response?

Adam

Inconsistency

First I want to thank you, your code has been a great help.
Second it all works fine but the search API is doesn't not always return tweets as it is returning null most of the time. Any suggestions?

The return is always NULL

Hi,
I'm making a code to take users, but the return is always NULL.

Below is my code:

"214180610-txxwvuHid4QK1vuYKLzhE4EbB4ERiDMXePryg7Ig", 'oauth_access_token_secret' => "jp1yqjgTNje9PyeqxOI8eZbO1KdO28UWMgElinvlSs", 'consumer_key' => "GgmaWZuV0z4KhLtK6uadaA", 'consumer_secret' => "V0E5UZL952HXpIlb832gNqPXmXB2vTudp0RzarcZQ" ); //GET Request Example $url = 'https://api.twitter.com/1.1/users/search.json'; $getfield = '?q=Danilo&page=1&count=1'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); var_dump(json_decode($response)); ?>

Are there something wrong in the code? I put the codes examples, but the return is also NULL.
What should I do to fix this problem?

Thanks.

Stream API is not working properly

Hi
I am trying to use https://stream.twitter.com/1.1/statuses/filter.json endpoint , though i am passing the track keyword it says "No filter parameters found. Expect at least one parameter: follow track locations" .Can you please take a look at here http://fuzonmedia.com/twitter-api/tst.php

I am using the below code .Can you please take a look what is wrong

require_once('TwitterAPIExchange.php');

echo "Working..";

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://stream.twitter.com/1.1/statuses/filter.json';
$requestMethod = 'POST';

/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
"track" => "twitter,welcome",
);

/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
$rslt=$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();

echo "<textarea>$rslt</textarea>";

Looking to query specific items

First of all, thanks for writing the library, it's definitely helping me understand the Twitter API a little easier.

Second, I am looking to query a specific username, then display the following data;

Followers (followers_count)
Favorites (favourites_count)
Retweets (retweet_count)

but I can't seem to figure out how to get just these items. I browsed through another example asking for follower count, but the final reply didn't work for me, any ideas why? Or how to do this?

The error I get is;
Notice: Trying to get property of non-object in C:\inetpub\wwwroot\ether\index.php on line 22

Thanks in advance!

---CODE BELOW--

"REMOVED", 'oauth_access_token_secret' => "REMOVED", 'consumer_key' => "REMOVED", 'consumer_secret' => "REMOVED" ); $url = 'http://api.twitter.com/1.1/statuses/user_timeline.json'; $getfield = '?screen_name=studionoctis'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); $response = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); var_dump(json_decode($response)); $followers = json_decode($response); echo $followers -> followers_count; ?>

Doesn't work? How to debug?

I just put my token and key in settings and the rest were the same.
But nothing was printed. Anyone helps me find out the reason ?

MitM security vulnerability

You should never, ever disable SSL peer verification unless you're cool with me hijacking your twitter API credentials. But seriously, this is a really bad security practice. Curl uses a certificate authority file that should have no problem verifying the twitter cert. There's no reason to disable this.

Works great! Here's a simple site template to get you started.

No issues, just thanks and an example of what you can do.

Add the following to the TwitterAPIExchange.php file onto your server and you're all set. Thanks to the original author who created this Twitter authentication code. Very much appreciated.

<?php
require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "#####",
    'oauth_access_token_secret' => "#####",
    'consumer_key' => "#####",
    'consumer_secret' => "#####"
);

$url = "https://api.twitter.com/1.1/search/tweets.json";

$requestMethod = "GET";

/** Find the recent tweet in Berkeley, CA within a two mile range (exclude links) **/
$getfield = '?q=-filter%3Alinks&result_type=recent&count=1&geocode=37.870108,-122.295304,2mi';

$twitter = new TwitterAPIExchange($settings);

$string = json_decode($twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(),$assoc = TRUE);
?>

<!doctype html>  
<html lang="en">  
    <head>  
        <title>@berkeley</title>  
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="60">
        <meta name="viewport" content="width=device-width; initial-scale=1.0;" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
        <style>
            html { margin:0; padding:0; border:0; font:normal 1em/1.5em 'Armata', 'Helvetica Neue', Helvetica, Arial, sans-serif; color:#ddd; }
            body { background:#555; }
            h1 { margin:0 0 10px 0; font:normal 3em/1.5em 'Armata', 'Helvetica Neue', Helvetica, Arial, sans-serif; }
            p { font-size:20px; }
            .alert-box { display:block; left:10%; top:20%; width:80%; position:absolute; }
            .tweet { clear:all; position:relative; }
            .user { margin-top:10px; float:left; font-size:1.1em; }
            .user img { height:32px; width:32px; margin:0 0 0 42px; vertical-align:middle; }
            .divider { position: relative; width: 100%; height: 0; padding: 0; border: 2px solid #999; margin-bottom:15px; }
            .divider:before { content: ''; position: absolute; border-style: solid; border-width: 15px 15px 0; border-color: #999 transparent; display: block; width: 0; z-index: 1; bottom: -15px; left: 31px; }
            .divider:after { content: ''; position: absolute; border-style: solid; border-width: 12px 12px 0; border-color: #555 transparent; display: block; width: 0; z-index: 1; bottom: -9px; left: 34px; }
            @media screen and (max-width:980px) { .alert-box { top:15%; } h1 { font-size:2.2em;} }
            @media screen and (max-width:520px) { .alert-box { top:20%; } h1 { font-size:1.3em;} .user { font-size:.9em;} }
        </style>
        <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Armata" type="text/css">
    </head>  
    <body>
        <div class="alert-box">
            <div class="tweet"><h1><?php foreach($string as $items) { echo $items[0]['text']; } ?></h1></div>
            <div class="divider"></div>
            <div class="user"><?php foreach($string as $items) { echo $items[0]['user']['name']; } ?></div>
        </div>
    </body>  
</html> 

Can this be installed via composer?

I'm a little rusty with composer / autoloading. Is there a way to install this and have it autoloaded automagically in a silex application?

Set json string as value

I'm trying the following

$redis->command('set test: ' . json_encode($jsonArrayed));

But I get bad syntax error, any idea how to set a json string as a value?

NULL for all tested urls

Any attempt to use this following the instructions results in a NULL result. Can't comment on Stack Overflow as I don't have enough rep. Any ideas, apologies I know this is not the best place for this but I think your library looks great but can't get any result other than NULL.

How to upload images with direct url?

Image upload simply fails with direct url as input. It doesn't show any error.

This is what I am trying to do:


$image = "http://i.imgur.com/f5v6ath.png"

$url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
$requestMethod = 'POST';


/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
    'status' => "Testing mentions with images",
    'media[]' => "@{$image}"
);


$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)
                              ->setPostfields($postfields)
                              ->performRequest();
print_r($response);

But it does not work. And It does not show any error.

However it works for local images ($image = "image-file.png").

What am I doing wrong?

Multiple Feeds on one site(diff' pages). 2 different Twitter accounts

I have a website that has one successful twitter feed being displayed using twitter-api-php. The website is Wordpress powered and here is the code for the successful Twitter feed. The following file is where I set the Keys and Tokens etc. You'll notice that $twitter_data is where the data is stored.

/************ This file twitter.php
*************************************************/

require_once('TwitterAPIExchange.php');

$settings = array(
    'oauth_access_token' => "123456",
    'oauth_access_token_secret' => "123456",
    'consumer_key' => "123456",
    'consumer_secret' => "123456"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=MyScreenNameOne&count=2';
$requestMethod = 'GET';


$twitter = new TwitterAPIExchange($settings);
$twitter_data = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest(); 

/********* end file

This file is where the tweets are displayed.
/************ This file twitter.php
*************************************************/

require('library/twitter/twitter.php');

$twitter_data = json_decode($twitter_data);

foreach($twitter_data as $key => $value)
{
    $text = $value->text;

    echo $value->text;
    echo $value->screen_name;
    echo 'whatever I want to display from the tweet';
}

/********* end file

I then have another Twitter feed that I am trying to display by essentially using the same two files but with slightly changed names (twitter-two.php and sidebar-two.php).

The file sidebar-two.php is almost identical apart from at the top I require require('library/twitter/twitter-two.php');.

My twitter-two.php is also nearly identical apart from different Keys, Tokens and the screen name. I'm going to post the code anyway incase I'm missing something.

/************ This file twitter-two.php
*************************************************/

require_once('TwitterAPIExchange.php');

$settings = array(
    'oauth_access_token' => " 123456",
    'oauth_access_token_secret' => "123456",
    'consumer_key' => "123456",
    'consumer_secret' => "123456"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=MyScreenNameTwo&count=2';

$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$twitter_data = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

For some reason this twitter doesn't come up with anything. I have tried everthing I can think of to debug this. If I put the working Key, Tokens and MyScreenNameOne in twitter-two.php and Tweets came from MyScreenNameOne. I then changed them back and no dice. I spat out the var_dump of $twitter_data and $twitter in file twitter-two.php and got back this (below).

$twitter
object(TwitterAPIExchange)#586 (8) { ["oauth_access_token":"TwitterAPIExchange":private]=> string(51) " xxxxxx" ["oauth_access_token_secret":"TwitterAPIExchange":private]=> string(45) "xxxxxx" ["consumer_key":"TwitterAPIExchange":private]=> string(22) "xxxxxx" ["consumer_secret":"TwitterAPIExchange":private]=> string(43) "xxxxxx" ["postfields":"TwitterAPIExchange":private]=> NULL ["getfield":"TwitterAPIExchange":private]=> string(30) "?screen_name=MyScreenNameTwo&count=2" ["oauth":protected]=> array(9) { ["oauth_consumer_key"]=> string(22) "xxxxxx" ["oauth_nonce"]=> int(1393256254) ["oauth_signature_method"]=> string(9) "HMAC-SHA1" ["oauth_token"]=> string(51) "xxxxxx" ["oauth_timestamp"]=> int(1393256254) ["oauth_version"]=> string(3) "1.0" ["screen_name"]=> string(9) "MyScreenNameTwo" ["count"]=> string(1) "2" ["oauth_signature"]=> string(28) "xxxxxx =" } ["url"]=> string(55) "https://api.twitter.com/1.1/statuses/user_timeline.json" }

$twitter_data
object(stdClass)#5936 (1) { ["errors"]=> array(1) { [0]=> object(stdClass)#5937 (2) { ["message"]=> string(23) "Bad Authentication data" ["code"]=> int(215) } } }

Does anyone have any idea what could be causing my to receive no tweets?

when i try to use this library, it shows me this error.

{"errors":[{"message":"Sorry, that page does not exist","code":34}]}{"ids":[23661767,253979844,9519542,9044112,483010616,1662412544,17316566,14915769,216892835,158415878,1688156532,282676041,512410920,272993773,1244558743,22564002,20946759,1192697377,179621064,435297265,1678989912,18764251,437715594,399300829,87297672,29706843,44124390,233262402,1610919390,170300398,341574583,971368818,17872114,42696990,447935911,19604531,293123302,177324910,166641797,174784251,52097984,43213525,77973285,1589093892,19894734,1427453509,309835298,343283519,225360831,1349512878,1556500176,14978709,14173119,1178548536,1425948607,564493814,14735313,19625601,549477794,6522872,265623200,49492914,14288063,328990309,187667664,552791239,120125810,153427757,35199327,563738194,1106314022,314836829,39494669,201172138,19963279,1300749464,798008552,521134013,114875506,37663179,1106234755,1538208510,18713086,1513560290,369627599,20272638,15527007,118730076,26539097,488690911,1530295674,14596685,132301166,81387319,141324051,293662243,323243155,989929922,1356562003,277342007,538545548,20462273,13691922,1427317880,172810945,186901523,219199596,218152771,14302901,405677179,103035472,16336998,166581053,1479205873,19857029,523249048,162565543,249913228,28346282,36161762,632527417,260317588,28111142,191479805,967731896,18581910,204258784,51404400,7972472,14154975,26099282,101758621,15632491,148811438,18309260,75591736,18949678,420396906,277042857,93731447,18018651,1347630908,12874852,1365870865,1372978358,46556494,78818627,83899714,1066072754,1252741832,225458271,19593998,987245808,46166123,44999727,1017790302,957796657,267550355,57233728,231328225,312196182,90165810,274959153,81505819,69779349,427168353,534774023,32579910,12193312,199959789,274287775,249813943,287086995,135509200,236325874,289123838,165316240,531388613,180913889,54009735,77378884,118277799,361563937,302436168,68324304,84143573,599610972,109873992,64960784,201476842,525638847,125150684,505228144,461746100,211197148,99832294,420696840,76299478,134937550,587225799,432661433,358274408,145156114,392175182,192911404,78105419,245108754,124904491,160852126,146267988,447537943,63684092,158163780,418436674,248220953,310458440,361490646,95600533,236623139,47401498,358802248,154448046,394508942,474285641,347465273,577301303,210476630,408112858,230258018,90211244,44699520,156331200,414879891,23793108,76115097,335434991,77104002,186332773,374077546,22631667,78519590,278081526,246287281,173102089,98265594,134300797,185056738,141431273,189203811,450156141,241628197,105053073,338068375,420329953,429055924,109427758,66198588,273873611,17443564,444837010,198294179,260732404,421224859,1174760586,379266792,253099765,200264942,51468750,153584204,22146581,116730042,112794452,73697954,147475591,477673878,153198641,560795230,98199395,245148841,84227763,67568323,569777244,32827888,27016772,302306688,361319204,72962300,83005854,116487905,275681652,842480802,263289572,70491224,160516682,309436690,146245362,129567972,74056455,370263357,165134778,39024721,166707323,157969551,148336333,402684487,753453692,96445137,16535378,1173195186,190230128,69614709,46961683,248414788,460504188,255549718,168421070,137804838,15449771,16083097,374630818,352266860,50671474,38144568,103462477,19290507,112546093,99345569,215885212,72901433,166090462,106468613,241993663,233114803,33836524,61961069,139756132,62179496,377675967,175924965,43766155,153177889,312081214,69330602,348676429,23730150,32518425,190055309,634079676,287308953,78402015,14769946,331122260,211332712,202349846,60670124,1022032580,173850893,173538082,235258378,181761709,155540958,143675583,75990489,85099009,107925783,147693623,302901645,245685332,342177239,885300186,574884749,423287622,79628084,565583424,79028666,471547337,239663541,297920348,109863978,187784579,198773440,73756579,65054529,305872407,29451771,58954460,60136781,221084619,67585838,589572687,322708567,94092035,121745924,94553076,207044432,154029272,44353513,323447557,97126907,201493717,162149532,133011705,25279850,318785680,139091650,127603394,91809443,377662706,518119822,26701297,294357276,56013265,22274992,396678326,174409609,137324601,34212145,255226294,467697258,19729993,78608830,216985651,480767618,74830023,93016403,28252270,133629027,33803230,355137811,99574852,282328798,116914667,125544962,264571292,287390897,110763163,308224043,82160026,111245461,111841364,145766250,139858028,269574596,1004765154,307267558,143763851,361052749,405476622,570915416,398844490,728961816,327429130,94782822,341213930,83104637,138064150,119429322,418036128,18974965,130206550,123134611,147934579,118879398,157469325,142073904,15614420,171180292,37875806,239672176,93946240,491165278,297762691,14224305,328119738,148971566,177417677,69320917,205263383,45896913,233206297,333569319,267728649,286150107,297078131,172051212,44036254,155309766,946085923,96636207,72194224,86546004,142994117,285313125,375649776,45536738,159030997,253731953,279491055,39672069,179026767,150253133,74838214,82882627,137133632,193795346,101333957,91938808,121955869,846593928,19353957,297405245,493641796,111968177,548218452,305817483,268609274,80752378,359057710,130283410,296751335,190138839,476028022,195689910,503640528,34983951,251795073,31378661,219765121,80056378,233097627,1168672226,1147512168,1167634556,1147434715,1147188768,1151696245,1175653759,1166514104,1166526084,1146823782,1148842153,1165424658,1180570033,1147003980,1180665098,1146809696,1146870686,1152731617,1163603748,1144728026,1166744347,1144748136,1166515627,1144739352,1144725620,1178600803,890811404,146954759,75210144,23313894,20207789,1117207525,550114283,488527171,469117930,204341083,27948237,968518298,373425970,19659475,182771087,36178952,256783503,20060827,115510458,166064436,20484564,30719065,24711275,274470394,281676389,229170774,987901352,288017022,961284834,957557971,109095854,23535583,15377115,916121870,918956012,123060226,21065280,608547542,101318880,97713113,875308446,176017790,274104078,75704938,17992309,374047120,551047675,611687065,39229240,223811950,787319719,582611351,825117373,56887681,804995834,357641650,717743984,223334498,41118292,380958938,47586577,22985994,607328648,565199783,566964585,565248301,578753459,607315307,579067027,578854183,580014467,577915107,579091036,565443644,574749843,566409448,565467952,564881878,569372263,630252961,14325700,25989243,563452467,40872715,119761217,605802060,41077741,278679716,32499999,246283635,44763465,161296820,28130604,567453000,464829290,541978077,554505371,226578862,217927688,48846301,502179769,95541428,23476850,389999922,120953514,242270066,33051095,527000061,40357467,25320756,119753235,484122214,181535620,406475736,259205267,49681212,65727873,16085576,447647299,216578253,173823177,376343310,35201615,27507571,255568543,101745778,21726648,49942619,36052286,286874850,54359928,181943348,18816351,84156655,353544318,392626205,26685874,22189215,15589631,258756204,51560169,359988880,355956194,21574548,181737469,49581737,91314945,56484692,28001057,18203589,238020959,111638421,23480044,93876552,33172214,42587162,35547588,130534017,52625053,119609860,13740602,59804598,53950999,95354674,17354164,70493359],"next_cursor":0,"next_cursor_str":"0","previous_cursor":0,"previous_cursor_str":"0"}

POST gives me {"errors":[{"message":"Could not authenticate you","code":32}]}

Hi,

My GET calls works perfect but I cant seem to post no matter what I tried. I tried single word statuses, urlencoding etc.

GET (Works)

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json';
$requestMethod = 'GET';

/** GET fields required by the URL above. See relevant docs as above **/
$getfield = '?count=5';

/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
$txtTwitter = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();

POST (Not working)

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';

/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array('status'=> 'test',
'in_reply_to_status_id' => 'rvalsan'
);

/** Perform a POST request and echo the response **/
$twitter = new TwitterAPIExchange($settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();

Both use the same settings
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "token",
'oauth_access_token_secret' => "secret",
'consumer_key' => "key",
'consumer_secret' => "secret"
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.