Avatar of Jeff Darling
Jeff Darling
Flag for United States of America asked on

PHP Twitter post Twitter API

I'm attempting to use PHP to post a tweet to twitter using this class:

https://www.madebymagnitude.com/blog/how-to-automatically-send-tweets-in-php/

<?php
include('TwitterPoster.php');
$status = "Just Send This!";
TwitterPoster::tweet($status);
?>

Open in new window


It does not work.  I get this error:  

Fatal error: Call to a member function post() on a non-object in /home/polysync/public_html/activist/twitter/test/TwitterPoster.php on line 75

Open in new window


The source for TwitterPoster.php looks OK. I do not see the problem.

I removed the token info from this post.

<?php

# Define constants
define('TWITTER_USERNAME', 'x');
define('CONSUMER_KEY', 'x');
define('CONSUMER_SECRET', 'x');
define('ACCESS_TOKEN', 'x');
define('ACCESS_TOKEN_SECRET', 'x');
define('TWEET_LENGTH', 140);

class TwitterPoster {

    private static $library = 'TwitterOAuth';
    private static $twitter = NULL;
    private static $DBH = NULL;

    /**
     * connect: Create an object of the Twitter PHP API either TwitterOAuth
     * or twitter-api-php
     * @access private
     */
    private static function connect() {

        if(self::$library == 'TwitterOAuth') {

            include('TwitterOAuth.php');

            # Create the connection
            self::$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

            # Migrate over to SSL/TLS
            self::$twitter->ssl_verifypeer = true;

        } else {

            include('TwitterAPIExchange.php');

            self::$twitter = new TwitterAPIExchange(array(
                'oauth_access_token' => ACCESS_TOKEN,
                'oauth_access_token_secret' => ACCESS_TOKEN_SECRET,
                'consumer_key' => CONSUMER_KEY,
                'consumer_secret' => CONSUMER_SECRET
            ));

        }

    }

    /**
     * setDatabase: Pass in a PDO connection, if you've already got one
     * @param $database PDO
     */
    public static function setDatabase($database) {
        self::$DBH = $database;
    }

    /**
     * tweet: Post a new status to Twitter
     * @param $message string
     * @access public
     */
    public static function tweet($message = '') {

        if(empty($message)) {
            return;
        }

        # Load the Twitter object
        if(is_null(self::$twitter)) {
            self::connect();
        }

        if(self::$library == 'TwitterOAuth') {
            echo $message;
            $response = $twitter->post('statuses/update', array('status' => $message));
            echo $response;
        } else {
            $url = 'https://api.twitter.com/1.1/statuses/update.json';
            $requestMethod = 'POST';
            $postData = array('status' => $message);
            $response = $twitter->buildOauth($url, $requestMethod)->setPostfields($postData)->performRequest();
        }

        return $response['id'];

     }

     /**
      * randomTweet: Send a random tweet from your database connection
      * @access public
      */
     public function randomTweet() {

	 $status="Just Send this!";
	 self::tweet($status);

	 }


 }

?>

Open in new window

TwitterPHP

Avatar of undefined
Last Comment
Jeff Darling

8/22/2022 - Mon
Mukesh Yadav

The post method is not defined in any class.
Jeff Darling

ASKER
It is defined in TwitterOAuth.php.  It appears to be unavailable.

I'm closer, but now I am not sure why this object didn't get created.

  /**
   * POST wrapper for oAuthRequest.
   */
  function post($url, $parameters = array()) {
    $response = $this->oAuthRequest($url, 'POST', $parameters);
    if ($this->format === 'json' && $this->decode_json) {
      return json_decode($response, true);
    }
    return $response;
  }

Open in new window

ASKER CERTIFIED SOLUTION
Mukesh Yadav

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeff Darling

ASKER
Answered to my satisfaction!  nice work.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck