Tweeting from your AppGini-generated web application

The tweeting method explained here is obsolete and no longer works with Twitter. We'll be updating this page soon. Please accept our deep apologies for any inconvenience caused by this.

Post to from your AppGini application So, you created a cool application with AppGini. Now you want to post a new Tweet whenever you add new records to your application. Here you'll learn how to do so. This tutorial requires PHP 5 or higher and AppGini 4.50 or higher.

What we want to do

Let's assume we've just created a real estate listings web site with AppGini. Now, we want to automatically tweet each new listing we add.

The listings_after_insert() hook is our friend!

We need to edit the listings_after_insert() hook. This hook function can be found in the generated hooks/listings.php file (assuming our table is named listings). We'll add code to post a tweet about each new listing added to our database. Here is the hook as generated by AppGini:
	function listings_after_insert($data, $memberInfo, &$args){
		return TRUE;
Let's modify this hook to:
	function listings_after_insert($data, $memberInfo, &$args){
		// detect the URL of the new listing
			$host = $_SERVER['HTTP_HOST'];
			$uri = rtrim($_SERVER['PHP_SELF'], '/\\');
			$http = (strtolower($_SERVER['HTTPS']) == 'on' ? 'https:' : 'http:');
			$url="$http//$host$uri?SelectedID=" . urlencode($data['selectedID']);

		// post the tweet, which includes the listing title and URL
		// posting will happen only if the tweet is 140 characters or less
			$post = "{$data['title']}. More info at $url";
			if(strlen($post)<=140) tweet('username', 'password', $post);

		return TRUE;
You should modify username and password in line 11 above to your actual Twitter username and password. To avoid trimming long posts, we make a check in line 11 to make sure our post won't exceed the tweet limit of 140 characters.

The final step is to define the tweet() function we used in line 11. So, let's put that function in the generated hooks/__global.php file to make it available to all other tables of our application. Let's open that file in a text editor and append the following code to the end of the file:
// tweet() function, courtesy of

function tweet($user, $pass, $tweet)
	$url = 'http://'.$user.':'.$pass.'';
	$post =  http_build_query(array ('source' => 'AppGini', 'status' => $tweet));
	$context = stream_context_create( array('http' => array('method' => 'POST', 'content' => $post)) );
	$connection = @fopen($url, 'rb', false, $context);
	if(!$connection) return false;

	return true;
The tweet() function above was adapted from the code example at

So, now each new listing we add to our database will be automatically posted to Twitter, together with a link to the listing details. Here is an example Twitter posting below.