April 5th, 2009 / by Wibeset / 0 comment

Mini client PHP5 pour mettre à jour votre statut sur Twitter:

<?php

/**
 * Twitter client
 *
 * @see Twitter
 * @see Twitter API documentation
 *
 */

class twitter {

  protected $url = "http://twitter.com/";

  /**
   * cURL instance
   */
  protected $ch = null;

  /**
   * Authentication
   */
  protected $username = '';
  protected $password = '';
  protected $authentication = '';

  /**
   * Constructor
   */
  public function __construct() {
    $this->ch = curl_init();
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->ch, CURLOPT_HEADER, 0);
    curl_setopt($this->ch, CURLOPT_VERBOSE, 0);
  } // __construct()

  /**
   * Destructor
   */
  public function __destruct() {
    curl_close($this->ch);
  } // __destruct()

 /**
   * Set user authentication
   *
   * @param username (string)
   * @param password (string)
   *
   */
  public function set_authentication($username, $password) {

    $this->username = $username;
    $this->password = $password;
    $this->authentication = $this->username.':'.$this->password;

  } // set_authentication()

  /**
   * Update the authenticating user's status
   * Authentication must be setted.
   *
   * @param status_text (string) new user's status
   *
   */
  public function update($status_text) {

    curl_setopt($this->ch, CURLOPT_URL, $this->url."statuses/update.json");
    curl_setopt($this->ch, CURLOPT_USERPWD, $this->authentication);
    curl_setopt($this->ch, CURLOPT_POST, 1);
    curl_setopt($this->ch, CURLOPT_POSTFIELDS, array('status' => $status_text));

    $result = curl_exec($this->ch);

    return json_decode($result, true);

  } // update()

} // twitter

Demo:

 $twitter = new twitter();
  $twitter->set_authentication('username', 'password');
  $twitter->update('hello from my new php5 client :)');

Pré-requis: cURL enabled et PHP >= 5.2.0

Leave a Reply

required
required
Comment: