Solved

PHP Twitter post Twitter API

Posted on 2016-08-08
4
148 Views
Last Modified: 2016-08-09
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

0
Comment
Question by:Jeff Darling
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Mukesh Yadav
ID: 41748234
The post method is not defined in any class.
0
 
LVL 12

Author Comment

by:Jeff Darling
ID: 41748589
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

0
 
LVL 8

Accepted Solution

by:
Mukesh Yadav earned 500 total points
ID: 41748603
Hi Jeff,

Just open TwitterOAuth.php and replace $twitter-> with self::$twitter->  in tweet function.

It's an typo in code i will report to the developer so that if anybody uses this code don't get stuck just because of a small type of developer.

Thanks
0
 
LVL 12

Author Closing Comment

by:Jeff Darling
ID: 41748780
Answered to my satisfaction!  nice work.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check mysql insert 12 38
issue with DB import 1 36
PHP: Filling Out/Creating a PDF 29 92
restriction of entering a a page 5 29
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question