Codebird library code works outside, but not inside a function - php

I want to make a function that will send automatic tweet when ever I publish some post on WordPress website. So I have included Codebird library inside functions.php, but I have problem with creating a function for tweeting. The tweeting function doesn't work.
function post_to_twitter($message)
{
$consumer_key = '....';
$consumer_secret = '....';
$access_token = '....';
$access_secret = '....';
\Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken($access_token, $access_secret);
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);
}
$message = 'New post published';
post_to_twitter($message);
When I put it outside the function, it works, like this
$consumer_key = '....';
$consumer_secret = '....';
$access_token = '....';
$access_secret = '....';
$message = 'New post published';
\Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken($access_token, $access_secret);
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);

Related

Get last tweet Twitter api

I try to show the last tweet from a specif user, using the Twitter API. When I try to echo the first result I get an empty page. When I echo my JSON data is see this.
I have the following PHP script:
<?php
require "Twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumer_key = "";
$consumer_secret = "";
$access_token = "";
$access_token_secret = "";
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");
$statuses = $connection->get("statuses/user_timeline", ["screen_name" => "NS_online"], ["lang" => "NL"]);
//var_dump($statuses);
// get your json as an associative array
$jsonArray = json_decode($statuses, true);
print_r($jsonArray);
$test = $jsonArray[0]->created_at;
echo $test;
?>

Twitter API, posting on two accounts, only one gets postd

I have a working code that posts on twitter account via APi, using codebird. However, for some reason only the first account gets posted to, but not the second. No errors. Here's the php code:
function tweet($message) {
global $consumerkey, $consumersecret, $accesstoken, $accesssecret;
require_once('/home/curse/public_html/manage/twitter-php-sdk/src/codebird.php');
\Codebird\Codebird::setConsumerKey("$consumerkey", "$consumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("$accesstoken", "$accesssecret");
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);
}
tweet("$twextra $makelink");
function tweet2($message) {
global $siimconsumerkey, $siimconsumersecret, $siimaccesstoken, $siimaccesssecret;
require_once('/home/curse/public_html/manage/twitter-php-sdk/src/codebird.php');
\Codebird\Codebird::setConsumerKey("$siimconsumerkey", "$siimconsumersecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("$siimaccesstoken", "$siimaccesssecret");
$params = array(
'status' => $message
);
$reply = $cb->statuses_update($params);
}
tweet2("$twextra $makelink");*

How do you get user OAuth for the Tumblr API in PHP?

Using the default Tumblr v2 API, I'm able to connect to my application, and retrieve posts from my own account. However, I'd like users to be able to connect their own accounts. I've tried to figure this out but I'm not entirely sure how to use OAuth (im using this class). How is this done?
The code I'm using to retrieve dashboard posts is:
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$tumblr = new Tumblr\API\Client(
$consumerKey,
$consumerSecret
);
var_dump( $tumblr->getDashboardPosts() ); // using var_dump for testing purposes only
This code works, but it's only returning the code for my PERSONAL account.
I figured it out, thanks to Github user seejohnrun.
require_once 'include/util.php';
$consumerKey = 'XXX';
$consumerSecret = 'XXX';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// If we are visiting the first time
if (!$_GET['oauth_verifier']) {
// grab the oauth token
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// tell the user where to go
echo ' GO ';
$_SESSION['t']=$data['oauth_token'];
$_SESSION['s']=$data['oauth_token_secret'];
} else {
$verifier = $_GET['oauth_verifier'];
// use the stored tokens
$client->setToken($_SESSION['t'], $_SESSION['s']);
// to grab the access tokens
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// and print out our new keys we got back
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
echo "token: " . $token . "<br/>secret: " . $secret;
// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "<br/><br/>congrats " . $info->user->name . "!";
}

Twitter access to resource error after first connection

I successfully setup twitteroauth and posted a message. Now, when I try to post again, and every time AFTER THE FIRST successful message post, I get an error Your credentials do not allow access to this resource. (code 220);
Here's my code:';
$message = 'Test';
$apiKey = Ranger_Application_Util::getConfig('twitter_app_api_key');
$apiSecret = Ranger_Application_Util::getConfig('twitter_app_api_secret');
$consumerToken = Ranger_Application_Util::getStorageData('twitter_oauth_token');
$consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
$twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');
$_SESSIOM['oauth_token'] = $consumerToken;
$_SESSION['oauth_token_secret'] = $consumerSecret;
require_once 'twitter/twitteroauth.php';
$connection = new TwitterOAuth($apiKey,$apiSecret,$consumerToken,$consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$accessToken = $connection->getAccessToken($twitterOauthVerifier);
$_SESSION['access_token'] = $accessToken;
$parameters = array('status' => $message);
$status = $connection->post('statuses/update',$parameters);
Core::dump($status);
die();
The data I retrieve from getStorageData is values stored in the database that pertain to that specific user.
Unless you are storing the information from $accessToken to your database in a piece of code you have not posted, I believe the issue may be that you are getting the access token after your initial connection, but not using the permanent access information from the access token when trying to post to the connection. So any future requests will not be using the permanent credentials and are therefore rejected. Try something like this:
$message = 'Test';
$apiKey = Ranger_Application_Util::getConfig('twitter_app_api_key');
$apiSecret = Ranger_Application_Util::getConfig('twitter_app_api_secret');
$twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');
require_once 'twitter/twitteroauth.php';
$hasAccessToken = (
array_key_exists('oauth_token', $_SESSION) &&
array_key_exists('oauth_token_secret', $_SESSION));
if ($hasAccessToken)
{
$consumerToken = $_SESSION['oauth_token'];
$consumerSecret = $_SESSION['oauth_token_secret'];
}
else
{
$consumerToken = Ranger_Application_Util::getStorageData('twitter_oauth_token');
$consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
$connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$accessToken = $connection->getAccessToken($twitterOauthVerifier);
$consumerToken = $accessToken['oauth_token'];
$consumerSecret = $accessToken['oauth_token_secret'];
$_SESSION['oauth_token'] = $consumerToken;
$_SESSION['oauth_token_secret'] = $consumerSecret;
}
$connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$parameters = array('status' => $message);
$status = $connection->post('statuses/update', $parameters);
Core::dump($status);
die();
You may want to store the oauth token information in your database after receiving it from getAccessToken() rather than using sessions. Maybe you have a method like Ranger_Application_Util::setStorageData('twitter_oauth_token', $consumerToken)?
This
$_SESSIOM['oauth_token'] = $consumerToken;
^
should be
$_SESSION['oauth_token'] = $consumerToken;

Why facebook API is returning incorrect response?

I am using facebook API to post comment on a post and the comment is posted successfully. But in response facebook returns:
An unexpected error has occurred. Please retry your request later.
When the I check the post, the comment is posted.
Here is my code sinppet:
$post_id = $_POST['post_id'];
$user_comment = $_POST['user_comment'];
$user_information = UserSocials::model()->findByAttributes(array('user_id' => Yii::app()->user->id));
$access_token = $user_information->fb_access_token;
$app_id = 'xxx';
$app_secret = 'yyy';
Yii::import("application.extensions.facebooksdk.Facebook");
$config = array();
$config['appId'] = $app_id;
$config['secret'] = $app_secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$result=$facebook->api('/' . $post_id . '/comments', 'post', array(
'access_token' => $access_token,
'message' => 'Your message',
)
);
print_r($result);
I must get a success or failure response because I have to prompt the user that the comment was posted or not.
Can anyone tell where I am going wrong?

Categories