How to Auto Post on Twitter with PHP


Manually post to social networks is probably better than to use an automated method. But, if you post many new topics every day, automated post is practically inevitable.

Twitter, as almost any social network, brings an Application Programming Interface, Twitter API, in order to communicate with it programmatically. You cannot use Twitter API directly (using your username and password), but you have to create a Twitter App. Creating a Twitter App is easy (see below). After you create your app, you get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret and you have to use them as credentials to communicate using Twitter API.

Additionally, you need a library for your programming language (PHP in my case) to easily use Twitter API. There are many libraries available. In this tutorial I use Codebird-php.

How to Create a Twitter app

Step 1 - Go to https://dev.twitter.com/apps and sign in with your account

(click the thumb for full image)

Step 2 - Press 'Create a new application' button


(click the thumb for full image)

Step 3 - Complete the required fields and press 'Create your Twitter application' (in the bottom of the screen)

(click the thumb for full image)

Step 4 - Go to 'Settings' TAB and set Application Type to 'Read and Write'. Then press 'Update this Twitter application's settings'

(click the thumb for full image)

Step 5 - Go to Details TAB and press 'Create my access token' button

(click the thumb for full image)

Step 6 - Go to oAuth tool TAB and get your access tokens

(click the thumb for full image)

Which PHP library for Twitter to select?

You may use any library available (in PHP section) of twitter libraries.

ПОЛЕЗНО  Удаление файлов в папке старше N дней

I tested many of these libraries. The are all working well. I finally selected Codebird-php, because

  • it is well written and documented
  • you can easily post an image with this library

Get Codebird-php from Github or Sourceforge. There is a DEV branch (2.5.0) on Github, which works well (at least with my tests). But, I preferred to use the stable version 2.4.1 (MASTER branch).

Actually, you need two files

  • codebird.php (the main class)
  • cacert.pem (Validate Twitter SSL certificate)

Get and put them in the same directory.

Codebird Requirements

Post single tweet using php

Here is an example to post a single tweet

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');
 
\Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_AccessToken", "your_AccessTokenSecret");
 
$params = array(
  'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter'
);
$reply = $cb->statuses_update($params);

Post tweet with image using PHP

Here is an example to post a tweet with image

// require codebird
require_once('/path/to/codebird_php_v2.4.1/codebird.php');
 
\Codebird\Codebird::setConsumerKey("your_ConsumerKey", "your_ConsumerSecret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("your_AccessToken", "your_AccessTokenSecret");
 
$params = array(
  'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter',
  'media[]' => '/path/to/picture.png'
);
$reply = $cb->statuses_updateWithMedia($params);

Twitter 140 characters restriction

Although the total length is more than 140 chars, you can successfully tweet DIRECTLY, something like this

Automatically Post on Twitter with PHP using Codebird - Step by step tutorial http://www.pontikis.net/blog/auto_post_on_twitter_with_php #php #twitter

because, Twitter will shorten the URL for you, so the total lenght will become less than 140.

ПОЛЕЗНО  Функция определения реального IP

But, you cannot do it using the above code. So, keep your automatic tweets length less than 140 characters (use fewer words or shorten URL)

Automatically Post on Twitter with PHP using Codebird http://goo.gl/OZHaQD #php #twitter

источник
Подробнее: https://coding.dp.ua/php/1706-how-to-auto-post-on-twitter-with-php.html