codebird-php
codebird-php copied to clipboard
Authentication when run by cron
When I hit my script in a browser, everything goes fine. When cron runs the script, it builds the tweet, and carries out the database stuff, but the tweet never appears. My logs show error code 32 - "could not authenticate you". Here's the code, sorry for the long code entry, I thought I better post the whole thing since I have no idea what could be causing the problem...
<?php
$test = false;
$go_tweet = false;
function tweet($message,$image,$link,$queue_id) {
// add the codebird library
require_once('codebird/codebird.php');
// note: consumerKey, consumerSecret, accessToken, and accessTokenSecret all come from your twitter app at https://apps.twitter.com/
\Codebird\Codebird::setConsumerKey("xxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
//build an array of images to send to twitter
$reply = $cb->media_upload(array(
'media' => realpath($image)
));
//upload the file to your twitter account
$mediaID = $reply->media_id_string;
//build the data needed to send to twitter, including the tweet and the image id
$params = array(
'status' => $message,
'media_ids' => $mediaID
);
//post the tweet with codebird
$reply = $cb->statuses_update($params);
$log = print_r($reply, true);
$log_query = "INSERT INTO tweet_log (log) VALUES ('".pg_escape_string($log)."')";
$log_result = pg_exec($link,$log_query);
$u_query = "UPDATE tweet_queue SET sent = 1 WHERE id = ".$queue_id;
$u_result = pg_exec($link,$u_query);
}
$host = "xx.xx.xx.xx.xx";
$port = "xxxxxxxxxxxxx";
$DbName = "xxxxxxxxxxxx";
$user = "xxxxxxxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$link = pg_connect("host=".$host." port=".$port." dbname=".$DbName." user=".$user." password=".$password);
//look for a tweet scheduled for NOW
$t_query = "SELECT * FROM tweet_queue WHERE post_time = '".date("Y-m-d H:i").":00' AND sent = 0 LIMIT 1";
$t_result = pg_exec($link,$t_query);
$t_numrows = pg_numrows($t_result);
if($t_numrows>0){
$t_raw = pg_fetch_assoc($t_result);
$tweet = $t_raw['tweet'];
$img = $t_raw['img'];
$queue_id = $t_raw['id'];
$go_tweet = true;
}else{
//tweet the next tweet in the queue
$t_query2 = "SELECT * FROM tweet_queue WHERE sent = 0 ORDER BY id ASC LIMIT 1";
$t_result2 = pg_exec($link,$t_query2);
$t_numrows2 = pg_numrows($t_result2);
if($t_numrows2>0){
$t_raw = pg_fetch_assoc($t_result2);
$tweet = $t_raw['tweet'];
$img = $t_raw['img'];
$queue_id = $t_raw['id'];
$go_tweet = true;
}else{
//nothing to tweet
$go_tweet = false;
}
}
//send the tweet
if($test){
echo 'will tweet: '.$tweet.'<br>will add image: '.$img;
}else{
if($go_tweet){
tweet($tweet,$img,$link,$queue_id);
}
}
?>
I've also tried:
require_once(__DIR__ .'/codebird/codebird.php');
Does the CLI PHP load the same required extensions as the web server one?
I am facing the same problem, during media_upload API call, it always sends "could not authenticate" errors.
@redbeanbuns @RahulRawat1994 You can help us find the problem by posting a self-contained code that will read the image from the same folder and send the media/upload call, and pasting the entire code here (masking your tokens).