Giter VIP home page Giter VIP logo

twitterautotweets's Introduction

TwitterAutoTweets

TwitterAutoTweets is a PHP script that helps you to post a tweet randomly based on your tweet collection. You can add as many as you want tweets and the later script will pick one random tweet and will post it on your timeline.

Create a Twitter app >> Add your tweets, hashtags and handles >> Set Cronjob and done

The idea is to set the tweets and set the cronjob to run the script. Cron will run in every 20 minutes and script will be executed to pick and post a random tweet from the collection Simple PHP Script to POST Tweets Automatically for Twitter API v1.1 calls. Script is based on [TwitterAPIExchange] (https://github.com/J7mbo/twitter-api-php) 

The aim of this script is simple. You need to:

  • Post tweets in a certain interval
  • Want to automate tweets if you have lots of tweets to share
  • Your tweets will be picked randomly and will be post

You really can't get much simpler than that. The above bullet points are an example of how to use the script for a POST request to make a Tweet.

Installation

Normally: If you don't use composer, don't worry - just include TwitterAPIExchange.php in your application.

require_once('TwitterAPIExchange.php');

Via Composer:

composer require j7mbo/twitter-api-php

How To Use

Set access tokens

$config = array(
    'oauth_access_token'        => "YOUR_OAUTH_ACCESS_TOKEN",
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
    'consumer_key'              => "YOUR_CONSUMER_KEY",
    'consumer_secret'           => "YOUR_CONSUMER_SECRET"
);

Below endpoint will post a tweet

$url = 'https://api.twitter.com/1.1/statuses/update.json';
$request_method = 'POST';

POST method

$post_fields = array(
    'status' => 'Hello! Twitter, How are you today?'
);

Perform the request!

$twitter = new TwitterAPIExchange($config);
echo $twitter->buildOauth($url, $request_method)
    ->setPostfields($postfields)
    ->performRequest();

GET all your tweets

require_once('TwitterAPIExchange.php');

$config = array(
    'oauth_access_token'        => "YOUR_OAUTH_ACCESS_TOKEN",
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
    'consumer_key'              => "YOUR_CONSUMER_KEY",
    'consumer_secret'           => "YOUR_CONSUMER_SECRET"
);

/* ---------------------------------------------------------------------------- */
// Sample to fetch user id
/* ---------------------------------------------------------------------------- */

// name whom you want to get userid
$username = 'TwitterIndia';
// endpoint to get userid by username
$url = "https://api.twitter.com/2/users/by";
// set field
$getfield = "?usernames=$username";
// set api method
$requestMethod = 'GET';

// twitter config
$twitter = new TwitterAPIExchange($config);
// pass teh filed
$get_response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();

// get json response and convert as array
$get_response = json_decode($get_response);

echo '<pre>', print_r($get_response, 1);

$user_id = NULL;

// if no error in response
if(!isset($get_response->errors))
{
    // extract user id
    $user = array_shift($get_response->data);
    // set user id
    $user_id = $user->id;

}else{
    die( 'invalid request or parameter' );
}

/* ---------------------------------------------------------------------------- */
// Sample to fetch all tweets by a user
/* ---------------------------------------------------------------------------- */

$url =  "https://api.twitter.com/2/users/{$user_id}/tweets";

// set field
$getfield = "?max_results=10";

$get_response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();

// get json response and convert as array
$get_response = json_decode($get_response);

// if no error in response
if(!isset($get_response->errors))
{
    echo '<pre>', print_r($get_response, 1);

}else{
    die( 'invalid request or parameter' );
}

Creating a new Twitter application

Creating A Twitter Application Thank you :)

twitterautotweets's People

Contributors

noncent avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.