How to Auto Post on Twitter using PHP API

How to Auto Post on Twitter using PHP API

Today, We want to share with you How to Auto Post on Twitter using PHP API.
In this post we will show you share post on twitter source code in php, hear for post tweet using twitter api we will give you demo and example for implement.
In this post, we will learn about twitter api post tweet php with an example.

Create a Twitter Application

Hello friends, You Need fisrt of all the create a Twitter application, So Here Link to click step By step Create a Twitter Devloper Accounts and the start a source code for Auto Post on Twitter using PHP

  Vuejs nested components
twitter api create token - How to Auto Post on Twitter using PHP API
twitter-api-create-token

Example : Auto Posting On Twitter with PHP

here We show You step By step Send Twitter Automatically Tweets using PHP Main 2 Example such as..

  1. Single tweet using php
  2. Tweet with image using PHP

Introduction To Auto Post on Twitter with PHP

How to Auto Post on Twitter using PHP API - How to Auto Post on Twitter using PHP API
How to Auto Post on Twitter using PHP API

Post single tweet using php

Now, This is an example to Auto post a wall on twitter in single tweet with status for Auto Post on Twitter.

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');
 
\Codebird\Codebird::setConsumerKey("xvz1evFS4wEssEPTGEFPHBog", "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2ddVS4cg");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("370773112-GmHxMAgY12345ZeRNFsMKPR9EyMZeS9weJAEb", "tnnArxj06cWHq44gCs15OSKk/jLY=
");
 
$arguments = array(
  'status' => 'Simple Example Of the Auto Post on Twitter with PHP http://pakainfo.com #php #twitter'
);
$reply = $cb->statuses_update($arguments);

Post tweet with image using PHP

Lets start, PHP API auto posting with Images on Twitter Account is an example to post automatically tweet status with image.

// require Libs codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');
 
\Codebird\Codebird::setConsumerKey("xvz1evFSss4wEEPTGEFPHBog", "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pddTgmZeNu2VS4cg");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("370773112-GmHxMAgYyLbN12eRNFsMKPR9EyMZeS9weJAEb", "tnnArxj066cWHq44gCs1OSKk/jLY=");
 
$arguments = array(
  'status' => 'Auto Post on Twitter with PHP http://pakainfo.com #php #twitter',
  'media[]' => '/path/to/pakainfo_logo.png'
);
$reply = $cb->statuses_updateWithMedia($arguments);

Auto Post on Twitter with PHP

<?php

if(php_sapi_name() == 'cli') {
  $dt_line_break = PHP_EOL;
} else {
  $dt_line_break = '<br>';
}
 
require_once('/path/to/codebird_php_v2.4.1/codebird.php');
 
$now =  date("YmdHis");
 
// initialize
$twitter_auto_share_unit = array();
 
// connect to database
$conn = new mysqli("localhost", "admin", "[email protected]", "auto_post");
 
// check connection
if ($conn->connect_error) {
  trigger_error('My sql Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}
 
// make array with topics to be auto posted on Twitter
$sql = 'SELECT id as topic_id, tw_article, tw_picture, tw_active FROM topics ' .
  'WHERE tw_date IS NOT NULL AND tw_date <= ' . "'" . $now . "' " .
  'AND tw_active = 0 ' .
  'ORDER BY tw_date ASC';
 
$results = $conn->query($sql);
if($results === false) {
  $user_error = 'Wrong SQL: ' . $sql . '<br>' . 'Error: ' . $conn->errno . ' ' . $conn->error;
  trigger_error($user_error, E_USER_ERROR);
}
$results->data_seek(0);
while($data_res = $results->fetch_assoc()) {
  $a_topic = array(
    "topic_id" => $data_res["topic_id"],
    "tw_article" => $data_res["tw_article"],
    "tw_picture" => $data_res["tw_picture"],
    "tw_active" => $data_res["tw_active"]
  );
  array_push($twitter_auto_share_unit, $a_topic);
}
$results->free();
 
\Codebird\Codebird::setConsumerKey("your_live_ConsumerKey", "your_live_Consumer_Secret_key");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_twitter_long_AccessToken", "your_AccessToken_keySecret_key");
 
// AUTOMATIC TWEET EACH TOPIC
foreach($twitter_auto_share_unit as $tapostshare) {
 
  if($tapostshare['twitter_status'] == 0) {
 
    if($tapostshare['tw_picture']) {
      $arguments = array(
        'status' => $tapostshare['tw_article'],
        'media[]' => $tapostshare['tw_picture']
      );
      $reply = $cb->statuses_updateWithMedia($arguments);
    } else {
      $arguments = array(
        'status' => $tapostshare['tw_article']
      );
      $reply = $cb->statuses_update($arguments);
    }
 
      // check if tweet successfully posted
    $status = $reply->httpstatus;
    if($status == 200) {
 
      // mark topic as tweeted (ensure that it will be tweeted only once)
      $sql = 'UPDATE topics SET tw_active = 1 WHERE id = ' . $tapostshare['topic_id'];
      if($conn->query($sql) === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
      }
      $response .= $tapostshare['topic_id'] . ' ' . $tapostshare['tw_article'] . ' success (' . $status . ')' . $line_break;
 
    } else {
      $response .= $tapostshare['topic_id'] . ' ' . $tapostshare['tw_article'] . ' FAILED... (' . $status . ')' . $line_break;
    }
 
    sleep(3);
  }
 
}
 
if(php_sapi_name() == 'cli') {
  // Take a log file
  file_put_contents('/your_path/to/twitter_auto_share.log', $response . str_repeat('=', 80) . PHP_EOL, FILE_APPEND);
   
  echo $response;
 
} else {
  $source_code = '<html><head><title>Auto Post on Twitter</title>';
  $source_code .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  $source_code .= '</head>';
  $source_code .= '<body>';
  $source_code .= $response;
  $source_code .= '</body>';
  $source_code .= '</html>';
  echo $source_code;
}
 
?>

Read :

  Vue js 2 Autocomplete Component

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Auto Tweet Twitter.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of