Category : "Coding"

All kinds of Programming.

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). ...

Server and client example with C sockets on Linux

In a previous example we learnt about the . In this example we shall build a basic ECHO client and server. The server/client shown here use TCP sockets or SOCK_STREAM. Tcp sockets are connection oriented, means that they have a concept of independant connection on a certain port which one application can use at a time. The concept of connection ...

Java socket programming tutorial

Socket programming in Java This is a quick guide/tutorial to learning socket programming in Java. The Socket class is documented at http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html To summarise the basics, sockets are the fundamental "things" behind any kind of network communications done by your computer. For example when you type www.google.com in your web browser, it opens a socket and connects to google.com to fetch the page ...

Python socket – network programming tutorial

Network programming in python This is a quick guide/tutorial on socket programming in python. Socket programming python is very similar to C. To summarise the basics, sockets are the fundamental "things" behind any kind of network communications done by your computer. For example when you type www.google.com in your web browser, it opens a socket and connects to google.com to fetch the ...

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 ...

Pages:«1...567891011...19»


Connect with us