Category Archives: Php Snippets

Php code snippets and examples

Fill text templates using arrays in php

By | September 22, 2012

Text templates are often used to generate content like notification emails, invoices filled with the details of a customer or user. The best example would be a bulk email program that sends out emails to multiple users filling the details of the particular user in every individual mail. Text templates have place holders for fields… Read More »

Php : Get name and value of all input tags on a page with DomDocument

By | August 4, 2012

The following code snippet will extract all input tag names and values as an associative array, from a given html page. [sourcecode language=”php”] /* Generic function to fetch all input tags (name and value) on a page Useful when writing automatic login bots/scrapers */ function get_input_tags($html) { $post_data = array(); // a new dom object… Read More »

Php : Fetch unread gmail emails using CURL

By | August 4, 2012

The following code snippet will fetch all unread gmail emails of a given account in xml(atom) format. It does this by simply fetching the page https://mail.google.com/mail/feed/atom using http authentication curl -u gmail_username –silent "https://mail.google.com/mail/feed/atom" /* To use this code, fill the correct values for the variables $gmail_username, $gmail_password */ function get_gmail($username , $password) { $url… Read More »

Php function to get the number of twitter followers

By | August 4, 2012

The following code snippet will fetch the number of twitter followers of a given twitter username /* This function will fetch the number of twitter followers of a twitter id It works by fetching an xml page for the user’s information */ function get_followers($twitter_id) { $xml = file_get_contents(‘http://twitter.com/users/show.xml?screen_name=’.$twitter_id); $s = simplexml_load_string($xml); $follower_count = $s->followers_count; return… Read More »

Execute shell commands in PHP

By | March 27, 2012

Like any other language php applications often need to execute system commands like they are run from the terminal/console/commandline. Php has multiple functions to do this task. Lets take a look at each of these 1. system “system() is just like the C version of the function in that it executes the given command and… Read More »