Category : "PHP"

Php tutorials

Php tutorial – Parse html DOM with DOMDocument

Domdocument The domdocument class of Php is a very handy one that can be used for a number of tasks like parsing xml, html and creating xml. It is documented here. In this tutorial we are going to see how to use this class to parse html content. The need to parse html happens when are you are for example writing scrapers, ...

UDP socket programming in php

In a previous article we learnt about writing simple server and clients using TCP sockets in php. In this article we are going to use udp sockets for the same. UDP sockets are much simpler to work with since they are connection-less. A udp server just has an socket that waits to receive some data and a socket client can send ...

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

The following code snippet will extract all input tag names and values as an associative array, from a given html page. /* 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 $dom = new DomDocument; //load the html into the object ...

Php : Fetch unread gmail emails using CURL

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 = "https://mail.google.com/mail/feed/atom"; $c = curl_init(); $options = array( CURLOPT_HTTPAUTH => CURLAUTH_BASIC , CURLOPT_USERPWD => "$username:$password" , CURLOPT_SSLVERSION => ...

Php : Get links on a page with DomDocument

Scraper scripts often need to extract all links on a given page. This can be done in a number of ways like regex, domdocument etc. Here is simple code snippet to do this using domdocument. /* Function to get all links on a certain url using the DomDocument */ function get_links($link) { //return array $ret = array(); /*** a new dom object ***/ $dom = new domDocument; /*** get the HTML ...

Php function to get the number of twitter followers

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 $follower_count; } /* Enter your twitter id here to test the code */ $twitter_id = 'linux'; echo "$twitter_id has ". get_followers($twitter_id) ...

Php function to add st, nd, rd, th to the end of numbers

Often numbers are to be written with a proper suffix, like 1st, 2nd, 3rd, 15th, 21st and so on. So here is a quick function to do the same for a number. Technique 1 /* This function will add st, nd, rd, th to the end of numbers */ function ordinal($i) { $l = substr($i,-1); $s = substr($i,-2,-1); return (($l==1&&$s==1)||($l==2&&$s==1)||($l==3&&$s==1)||$l>3||$l==0?'th':($l==3?'rd':($l==2?'nd':'st'))); } /* Example usage */ for($i = 0; $i < 100; $i++) { echo $i . ordinal($i). ...

Javascript style object literals in PHP

Javascript object literals look like this var a = { 'var_1' : 'Hello' , 'var_2' : 'World' , 'get' : function() { alert(a.var_1); } }; As similar thing can be done in PHP using anonymous functions (since 5.3) Quick example $a = [ 'var_1' ...

PHP Socket programming tutorial

Php and tcp/ip sockets This is a quick guide/tutorial to learning socket programming in php. Socket programming php is very similar to C. Most functions are similar in names, parameters and output. However unlike C, socket programs written in php would run the same way on any os that has php installed. So the code does not need any platform specific ...

Implementing callbacks in php with anonymous functions

What are callbacks Callback is a piece of code that is passed to some other functions as a parameter, such that it can be executed before or after a certain event or change of state. A common example of callbacks can be seen in javascript when using the jquery ajax function. $.ajax({ url: 'ajax/test.html', success: function(data) { ...



Connect with us