I'm trying to use Codebird to show my latest tweet on a simple website. Unfortunately, I can't manage to make it work.
Here's what I did for now.
I created my App on Twitter Developer page. I obtained my Key/Secret and then my Token/Secret. Then I wrote my small PHP script and tried to show the timeline just to see if everything works. Here I encountered the problems. The code goes like this:
<?php
require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey(MY_KEY, MY_SECRET);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken(MY_TOKEN, MY_TOKEN_SECRET);
$reply = (array) $cb->statuses_homeTimeline();
print_r($reply);
?>
(and obviously I put the various key strings in the correct arguments).
This code gives my an Array ( [httpstatus] => 0 ). So I tried
print_r($reply[0]);
But then nothing is printed out in the page.
Where am I wrong? How should I modify this code to get my last tweet? I'm a bit new with the new Twitter API, and a lot of stuff confuses me.
Thank you for your help!
I copied and pasted your code and it's not working.
I get error regarding the CodeBird class
Try:
require_once ('codebird.php');
Codebird::setConsumerKey('key', 'secret key');
$cb = Codebird::getInstance();
$cb->setToken('token', 'secret token');
$reply = (array) $cb->statuses_homeTimeline();
this should work just fine.
<?php
use Codebird\Codebird;
require 'vendor/autoload.php';
$cb = new Codebird;
$cb->setConsumerKey(
'Consumer_Key',
'Consumer_Secret'
);
$cb->setToken(
'Access_Token',
'Access_Token_Secret'
);
$reply = (array)$cb->statuses_homeTimeline();
echo '<pre>';
print_r($reply);
Related
I've been trying to post an image with a simple message onto twitter using PHP and twitteroauth.php.
However, every time I run my code, I only get the $tweetMessage published on the twitter feed without any image.
I searched and searched and read their own documentation but don't even get me started on their own documentation! its like someone who's had a sleepwalk was writing their documentation. Just a bunch of jargon..
And most of the information on STO is either outdated or pointing to a library!
I do not want to use any library as I will have to try to learn someone else's code as well and Surely twitter would allow publishing photo's using their own API without the use of any third party Library?!
Any way, This is my full code:
// Include twitteroauth
require_once('inc/twitteroauth.php');
// Set keys
$consumerKey = 'xxxxxxxxxxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessTokenSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Create object
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
// Set status message
$tweetMessage = 'This is a tweet to my Twitter account via PHP.';
$image_path="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
$handle = fopen($image_path,'rb');
$image = fread($handle,filesize($image_path));
fclose($handle);
// Check for 140 characters
if(strlen($tweetMessage) <= 140)
{
// Post the status message
$tweet->post('statuses/update', array('media[]' => "{$image};type=image/jpeg;filename={$image_path}", 'status' => $tweetMessage));
}
Could someone please advise on this issue?
Thanks in advance.
EDIT:
I've changed my code to the following and I get this error:
{"errors":[{"code":195,"message":"Missing or invalid url parameter."}]}
But I'm sure the image is on the specified URL/directory!
This is the code:
require_once 'inc/twitteroauth.php';
define("CONSUMER_KEY", "xxxxxxxxxxxxxxxxx");
define("CONSUMER_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_TOKEN", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("OAUTH_SECRET", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('images/sign-in-with-twitter-l.png');
$image = 'images/sign-in-with-twitter-l.png';
$status_message = 'Attaching an image to a tweet';
$status = $connection->post('statuses/update_with_media', array('status' => $status_message, 'media[]' => file_get_contents($image)));
echo json_encode($status);
Any idea why this error is being shown?
Uploading media to Twitter is slightly complicated. Essentially, it's a three stage process.
Upload the photo to Twitter.
Receive a media_id back from Twitter.
Post your status and media_id to Twitter.
This is described in great detail at https://dev.twitter.com/rest/reference/post/media/upload
Generally speaking, it is easier for use to use a library like CodeBird as they've already done the hard work of finding all the edge cases.
But, assuming you don't want to do that...
POST the image to /1.1/media/upload.json
Receive back some JSON like
{
"media_id": 553656900508606464,
"media_id_string": "553656900508606464",
"size": 998865,
"image": {
"w": 2234,
"h": 1873,
"image_type": "image/jpeg"
}
}
* Use that media_id_string when you post the status. e.g.
tweet->post('statuses/update', array('media_ids' => $media_id_string, 'status' => $tweetMessage));
Hopefully that gives you enough to understand what's going on.
I solved it like this:
$tweet_img = 'Path/to/image';
$handle = fopen($tweet_img,'rb');
$image = fread($handle,filesize($tweet_img));
fclose($handle);
$parameters = array('media[]' => "{$image};type=image/jpeg;filename={$tweet_img}",'status' => 'Picture time');
$returnT = $connection->post('statuses/update_with_media', $parameters, true);
Horrible twitter API documentation needs improving!! it needs to be written by humans as opposed to a bunch of sleepwalking zombies!!!
This is a very frustrating situation that they put us in when we try to use their API...
They either need stop their API support and remove it all from the public or simply improve their documentation and write it for the public and not just for their own use using jargon words.
Any way, the above code works just fine using the latest twitteroauth
I hope this helps others in my situation.
I feel like i wasted 5 hours for something that should be clear and mentioned in plain English on their site!!!
Rant and Answer over & good luck.. :)
I am not a hardcore coder but i have small knowledge i have attempted to fix this error we are getting regarding the Twitter api
there is a line of code that checks that a twitter name is currect which is sent via a form
$url = get_data("http://api.twitter.com/1/users/lookup.json?screen_name=".$name);
$xml = json_decode($url, true);
$id = $xml[0]['id'];
$av = $xml[0]['profile_image_url'];
if ($id != "")
What i understand
I understand that this version of twitter is no longer available and i need to update it to 1.1
What searching and research i have done
I have Searched the net for a quick answer for this but was not able to find any....
I have found this code https://github.com/abraham/twitteroauth/tree/master/twitteroauth
My question
How do i adapt the files to work within this file? i didnt post the whole code within the page as im not sure all of it is relevant.
Just use the 1.1 in the URL. That will do
http://api.twitter.com/1.1/users/lookup.json
-------^
Do like this...
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN,OAUTH_SECRET);
$account = $connection->get('account/verify_credentials');
$status = $connection->post('statuses/update', array('status' => 'Text of status here', 'in_reply_to_status_id' => 123456));
$status = $connection->delete('statuses/destroy/12345');
Documentation
Ok, so following some instructions I found in another post here on StackOverflow, I have constructed a script to get a fan pages feed and turn it into an RSS2 feed. However, the script required a few changes and Im not the best programmer in the world, so I need a little help.
Im getting this error:
Warning: Invalid argument supplied for foreach() in feed.php on line 48
Im not sure what the invalid argument is all about.
<?
// error reporting
echo '<pre>';
ini_set('display_errors', 'on');
error_reporting(E_ALL);
// require your facebook php sdk
require('./facebook/facebook.php');
// include the feed generator feedwriter file
include("./feed/FeedWriter.php");
// config secret key and appid
$config = array(
'appId' => '',
'secret'=> ''
);
// Initialize
$facebook = new Facebook($config);
// Set Apps Permissions Request
$permission_scope = "";
// get users access token
$access_token = $facebook->getAccessToken();
// get page post
$feed_url = 'https://www.facebook.com/Ritualdubstep/feed?access_token='.$access_token;
$feed_json = file_get_contents($feed_url);
$feed_data = json_decode($feed_json);
// create the feedwriter object
$feed = new FeedWriter(RSS2);
$feed->setTitle('Ritual Dubstep'); // set your feed title
$feed->setLink('https://www.facebook.com/Ritualdubstep'); // set the url to the feed page you're generating
$feed->setChannelElement('updated', date(DATE_RSS , time()));
$feed->setChannelElement('author', array('name'=>'Ritual Dubstep SF')); // set the author name
// iterate through the facebook response to add items to the feed
foreach($feed_data['data'] as $entry){
if(isset($entry["message"])){
$item = $feed->createNewItem();
$item->setTitle($entry["from"]["name"]);
$item->setDate($entry["updated_time"]);
$item->setDescription($entry["message"]);
if(isset($entry["link"]))
$item->setLink(htmlentities($entry["link"]));
$feed->addItem($item);
}
}
// generate feed
$feed->genarateFeed();
?>
Generally it means that the first argument in the foreach call (in this case $feed_data['data']) is not a valid array.
Make sure that $feed_data['data'] exists (isset($feed_data['data'])) and that it is an array (is_array($feed_data['data'])) before running entering the foreach loop.
And - as shapeshifter mentioned in the comments - you might want to start troubleshooting by var_dump($feed_data['data']) right before you start the foreach loop to see what's being generated.
I want to get all the events of the logged in user, who has the app installed.
This is not intended to be a public app and is for me only. The user (me) has has given the app permissions like below:
I'm using this code:
<?php
// get all events for logged in fb user
require_once 'facebook.php';
$app_apikey = 'my-key-is-here';
$app_secret = 'my-secret-is-here';
$facebook = new Facebook($app_apikey, $app_secret);
$user_id = $facebook->require_login(); //returns my facebook user_id fine!
$events = $facebook->api_client->events_get($user_id); //returns nothing!
echo $events; //nothing
print_r($events); //no array to print
print_r(json_decode($events)); //no array to print
?>
Any ideas on why I'm getting a no results from events.get()?
It seems that you are using the old php-sdk? try using this one.
Make sure you have the user_events permission. Check this answer.
With the new SDK, it's as simple as:
$events= $facebook->api('/me/events');
im trying to update my news feed on facebook. Im using the new graph api. I can connect to graph, but when i try to publish some content to the feed object, nothing happens.
here´s my code:
<?php
$token = "xxxx";
$fields = "message=test&access_token=$token";
$c = curl_init("http://graph.facebook.com/me/feed");
curl_setopt($c,"CURLOPT_POST", true);
curl_setopt($c,"CURLOPT_POSTFIELDS",$fields);
$r = curl_exec($c);
print_r($r);
this returns:
{"error":{"type":"QueryParseException","message":"An active access token must be used to query information about the current user."}}1
then I try to pass access_token via GET:
$c = curl_init("http://graph.facebook.com/me/feed?access_token=$token");
this returns:
{"data":[]}1
Am I doing something wrong?
thanks
I found my error!
I was putting CURL options as a string rather than constants.
oopps...