21
Jun

PHP – Twitter quick and dirty – No class

by Michel Beaussart in PHP Coding, Twitter

twitter-logoYou have a database full of items you want to post on twitter. If you are not familliar with oauth or do not have a real use for it (you manage your accounts through scripting), basic auth is the way to go. Here is our quick and dirty poster:
This is just rough code, add the bells and whistles yourself.

The following table is assumed:

CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item_number` varchar(5) character SET ascii NOT NULL DEFAULT '',
  `description` varchar(50) character SET ascii NOT NULL DEFAULT '',
  `price` varchar(4) character SET ascii NOT NULL DEFAULT '',
  `url` text character SET ascii NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=144845 ;

Your PHP db function:

 
function agent_db()
{
$dbuser        = "your db username";
$dbserver    = "your host";
$dbpass        = "your db password";
$dbname        = "your db name";
 
//CONNECTION STRING
$db_conn = mysql_connect($dbserver, $dbuser, $dbpass)
or die ("UNABLE TO CONNECT TO DATABASE");
mysql_select_db($dbname)
or die ("UNABLE TO SELECT DATABASE");
return $db_conn;
}

Your Tweet Function:

function tweet($message, $username, $password)
{
$url = 'http://twitter.com/statuses/update.json';
$fld = http_build_query(array('status' => $message));
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fld);
 
$ret = curl_exec($ch);
 
return $ret;
}

Now let's loop on the items:

$sql = "SELECT * from items;"; // Limit 0,50 if you do not want to be marked by twitter - Go easy on mass posting
$result = mysql_query($sql,db());
while ($row = mysql_fetch_assoc){
$item_number = $row['item_number'];
$description = $row['description'];
$price = $row['price'];
$url = $row['url'];
 
$post = "$item_number $description $price $url"; // That can be anything ..just has to be 140 char .. which you should test before sending .. Add it ! <img src='http://www.michel-angelo.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
 
$tweet = tweet($username,$password,$post);
// you can test $tweet for error when the function is done returning the value, to see if the post was successfull or not;
echo "$tweet\n
";
 
}

Taaadaaa !
See Ma' no class!
You can also replace the tweet function by the following if you do not have the Curl library installed (Original Post from Fabien Potencier):

function tweet($message, $username, $password)
{
  $context = stream_context_create(array(
    'http' =&gt; array(
      'method'  =&gt; 'POST',
      'header'  =&gt; sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
                   "Content-type: application/x-www-form-urlencoded\r\n",
      'content' =&gt; http_build_query(array('status' =&gt; $message)),
      'timeout' =&gt; 5,
    ),
  ));
  $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context);
 
  return false !== $ret;
}
13
Jun

Agemni SOAP and PHP

by Michel Beaussart in Agemni CRM

Comments closed Comments

agemni Agemni (http://www.agemni.com) is a really neat CRM product for DirectTV, Dishnetwork and ADT reseller.

I am not here to describe all the advantages the reseller have working with this tool, but to talk about the different integration possibilities with existing feed, listings and leads databases.

Nowadays, it is not enough to have a great CRM and while Agemni has pretty much anything you might need and more, it still lacks the most important of all: The leads!

Well, they probably thought about it and gave to the world a SOAP API described in their wiki (http://wiki.agemni.com). Through the nusoap library and few calls we design a system that can integrate Agemni with a live source of leads, almost a on demand feed for your CRM.

Everything is done in PHP. A class will be released soon to be used for putting leads in and pulling data out.

You can open a trial account on Agemni and their staff is really helpful, though the documentation for their API is out dated and some of the functions we developed where more a guessing game than good old coding practice. Anyway, if you are interested about that PHP class for agemni, drop us a line : michel at michel-angelo dot net.