User Agent Parsing with PHP


Hits: 2856  

Tobie Langel’s ua-parser, a great library which provides simple UA parsing in PHP, JavaScript and Python.

<?php require_once 'uaparser.php';

$ua = $_SERVER['HTTP_USER_AGENT'];
$parser = new UAParser();
$result = $parser->parse($ua);

echo '<pre>';
print_r($result);

?>

Mistserver Manager Login

Find more libraries here...

https://github.com/rjd22/ua-parser/tree/master/php
https://github.com/Synchro/ua-parser

Upgrade PHP & MySQL on Plesk 10


Hits: 3048  

1. Stop Running Apache & MySQL Server
# /etc/init.d/mysqld stop
# /etc/init.d/httpd stop

2. Set up the atomic channel
# wget -q -O – http://www.atomicorp.com/installers/atomic | sh

3. Upgrade to PHP 5.3.x on plesk 10
# yum upgrade

Now I can install “uploadprogress” extension easily

pecl/uploadprogress requires PHP (version >= 5.2.0), installed version is 5.1.3
No valid packages found
install failed

# pecl install uploadprogress

Then add in /etc/php.ini

extension=uploadprogress.so

PHP Bulk Emailer


Hits: 4317  

If you send email to 5,000 people using standard PHP tools, your ISPs might be blocked in seconds.

I think you already read below articles…

Theory About Bulk Emails

Emails are not working on server?

Amazon SES – Emails In Cloud

Open source – Port25

Linux Mail Servers

Email’s Major Sections

Header – From, To, Subject, Date, Message-Id
Body – HTML / Text

Server must have …

* Dedicated public static IP address
* Reverse DNS record for IP address
* Contact info in WHOIS record
* Mailbox Tracker
* SPF Records
* Signed message using DKIM
* VERP – Variable envelope return path
* Automatic Bounce Handling
* Email Scheduler
* Email Subscribe / Unsubscribe
* Keep the IPs white-listed

Limit on servers

500/per hour on the Shared servers is the limit.

Other References

http://framework.zend.com/manual/en/zend.mail.html
http://pear.php.net/package/Mail_Queue
http://www.phplist.com/details
http://wiki.e107.org/index.php?title=Bulk_mailer_interface
http://swiftmailer.org/
http://sourceforge.net/projects/phpmailer/
http://mlmmj.org/
http://www.gnu.org/software/mailman/index.html

PHP PEAR File Download Package


Hits: 3491  

pear-download

The PEAR package contains:
* PEAR installer, for creating, distributing and installing packages
* PEAR_Exception PHP5 error handling mechanism
* PEAR_ErrorStack advanced error handling mechanism
* PEAR_Error error handling mechanism
* OS_Guess class for retrieving info about the OS where PHP is running on
* System class for quick handling of common operations with files and directories
* PEAR base class

Features in a nutshell:
* full support for channels
* pre-download dependency validation
* new package.xml 2.0 format allows tremendous flexibility while maintaining BC
* support for optional dependency groups and limited support for sub-packaging
* robust dependency support
* full dependency validation on uninstall
* remote install for hosts with only ftp access
* full support for mirroring
* support for bundling several packages into a single tarball
* support for static dependencies on a url-based package
* support for custom file roles and installation tasks

Traditional Code


<?php

$path = "svnlabs.csv";

if(file_exists($path))

{

$size = filesize($path);

header('Content-Type: application/octet-stream');

header('Content-Disposition: attachment; filename='.$path);

header('Content-Length: '.$size);

readfile($path);

}

?>

PEAR Code
Traditional Code sometime fails on some browsers or OS 😉
PHP Developer can trust PEAR 100% 😀


<?php

include_once("HTTP/Download.php");

$d = new HTTP_Download();

$d->setContentDisposition( 'HTTP_DOWNLOAD_ATTACHMENT', 'svnlabs.csv' ); // set file name

$d->setContentType( 'text/csv' ); // set file type

$d->setData($csv);

$d->send();

?>

PHP PEAR File Download Package

SVNLabs Tools


Hits: 2480  

HTML2CSV converts html/xhtml to csv/xls.

VMG2TXT converts Nokia VMG message file to text file.

VCard Parser Extract information from VCard.

HTML2RSS converts html/xhtml to rss/xml feed.

Amazon S3 upload file to Amazon S3 Bucket.

CSV Mapper Import CSV file after adjusting Columns.

JavaScript Twitter Bird

a2zVideoAPI – search videos with page link

EBook Search Engine

Google Map Info Window

PHP Syntax Highlighter.

Backup mysql database to amazon S3.

Pretty Print JSON


Hits: 3542  

We can use function json_print() to explore JSON data, since JSON has no spacing or indentation. The function json_print() make JSON data to display in the human-readable format.

<?php

function json_print($json) {

    $result      = '';
    $pos         = 0;
    $strLen      = strlen($json);
    $indentStr   = '  ';
    $newLine     = "\n";
    $prevChar    = '';
    $outOfQuotes = true;

    for ($i=0; $i<=$strLen; $i++) {

        // Grab the next character in the string.
        $char = substr($json, $i, 1);

        // Put spaces in front of :
        if ($outOfQuotes && $char == ':' && $prevChar != ' ') {
            $result .= ' ';
        }

        if ($outOfQuotes && $char != ' ' && $prevChar == ':') {
            $result .= ' ';
        }

        // Are we inside a quoted string?
        if ($char == '"' && $prevChar != '\\') {
            $outOfQuotes = !$outOfQuotes;

            // If this character is the end of an element, 
            // output a new line and indent the next line.
        } else if(($char == '}' || $char == ']') && $outOfQuotes) {
            $result .= $newLine;
            $pos --;
            for ($j=0; $j<$pos; $j++) {
                $result .= $indentStr;
            }
        }

        // Add the character to the result string.
        $result .= $char;

        // If the last character was the beginning of an element, 
        // output a new line and indent the next line.
        if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
            $result .= $newLine;
            if ($char == '{' || $char == '[') {
                $pos ++;
            }

            for ($j = 0; $j < $pos; $j++) {
                $result .= $indentStr;
            }
        }

        $prevChar = $char;
    }

    return $result;
}

// Uses....

//........
//........

echo json_print($json);


?>

Related JSON Function: json_add()

Other Resources:
http://pretty-print.org/
http://chris.photobooks.com/json/default.htm
https://gist.github.com/906036
http://jsonviewer.stack.hu/
http://www.cerny-online.com/cerny.js/demos/json-pretty-printing

Knowledge Base – It is little use to dig a well after the house has caught fire… SAVE knowledge like Money 😉

Amazon E-Commerce Service or ECS


Hits: 2166  

Amazon’s ECS is very good service for accessing Amazon’s product database. We can register to this web service quickly, It provides a free access key to access Amazon Store.

Amazon have rich set of web services 🙂 Web Services are used to access DATA over cross platform environments.

Using ECS-driven websites and applications, we can earn commissions by advertising items for sale by Amazon.

Amazon E-Commerce Service can be used to get the information from amazon such as product name, images, availability, ratings, description, specifications, similar products, and more.

We can use PHP to consume web service using PEAR, REST, SOAP and XML etc.

Try: https://github.com/Exeu/Amazon-ECS-PHP-Library

There is one thing which gives radiance to everything. It is the idea of something around the corner.

Override PHP Function


Hits: 3195  

PHP have PECL (PHP Extension & Community Library) function to override built-in functions by replacing them in the symbol table.

bool override_function ( string $function_name , string $function_args , string $function_code )

 

  1. <?php
  2. override_function(‘strlen’, ‘$string’, ‘return override_strlen($string);’);
  3. function override_strlen($string){
  4. return strlen($string);
  5. }
  6. ?>

The above function “override_function()” require APD i.e. Advanced PHP Debugger.

We can find more about APD here…
http://pecl.php.net/package/apd

Linux users can install apd using below command

# pecl install apd

There is an alternate way to override PHP functions, we can use below class “override” to override any built-in PHP function if PECL is not installed on server 😉

 

  1. <?php
  2. $or = new override ();
  3. $or->override_function(‘strlen’, ‘override_strlen‘, ‘return override_strlen($string);’);
  4. function override_strlen($string){
  5. return strlen($string);
  6. }
  7. ?>

 

 

<?php

$url = 'http://www.svnlabs.com';
$override = new override();

 

$override->override_function('file_get_contents','fileGetContents',$url);

if ($over_func_name = $override->override_check('file_get_contents')) {
    $result=call_user_func($over_func_name, $url);
}

function fileGetContents($url)
{
  /// statements
}

?>

 

PHP Function OverRide Class

<?php 


class override 
{ 

var $functions = array(); 
var $includes = array(); 

function override_function($override, $function, $include) { 
 if ($include) { 
 $this->includes[$override] = $include; 
 } 
 else if (isset($this->includes[$override])) { 
  unset($this->includes[$override]); 
 } 
 $this->functions[$override] = $function; 
} 


function override_check($override) { 
 if (isset($this->includes[$override])) { 
 if (file_exists($this->includes[$override])) { 
 include_once($this->includes[$override]); 
} 

if (function_exists($this->functions[$override])) {
 return $this->functions[$override]; 
} 
else 
{ 
 return false; 
} 
} 
else 
{ 
return false; 
} 
} 
} 

?>

Make a habit of creating things modular, that means “pluggable” and “unpluggable”.

a2zVideo API


Hits: 2122  

Welcome to the a2zVideoAPI wiki!

This API supports video links to get video information like Embed Code, Video Thumb, Video Title, Video Description etc.

How to use a2zVideoAPI:

1. Download “api.php” from http://github.com/svnlabs/a2zVideoAPI

2. API require PHP with CURL extension.. Setup / Upload “api.php” to your web server

3. Modify “api.php” for API Video server
curl_setopt($ch, CURLOPT_URL, ‘http://www.svnlabs.com/a2zvideoapi/server.php’);

4. There are currently 3 formats for API response

a. HTML
b. XML
c. JSON

$post = array( ‘url’ => “http://www.flickr.com/photos/schwartzray/2483697739/”, ‘format’ => “html” );

5. You have to use valid video page url like “http://www.youtube.com/watch?v=mXMf9GOzzOA” with format “html”, “xml” and “json”

6. Enjoy with svnlabs.com

Follow me….

Facebook: http://www.facebook.com/svnlabs
Twitter: http://www.twitter.com/svnlabs

Subscribe me….
Youtube: http://www.youtube.com/user/svnlabs
Feeds: http://blog.svnlabs.com/feed/

PHP Debug Log – Trace Errors


Hits: 3325  

Hello Friends,

I read some where “Quality is not a product.. it is a process 😉 ”

I think process is hard-work we do and product is final result we get.

But how we make our process to get a good product, as a LAMP developer I think track processes “Debug Log” is good sort of tool for monitoring programs.

Trouble shooting is quite simple with Log Files. Log file can be used with analysis tools, it’s possible to get a good idea of where errors are coming from, how often errors return.

Here you can see a simple PHP script to trace PHP programs…

debugLog.php
………………….

function debugLog($log, $text)
{
$log_dir = dirname($log);
if( !file_exists($log_dir) or !is_dir($log_dir) or !is_writable($log_dir) )
return false;

$write_mode = ‘w’;
if( file_exists($log) && is_file($log) && is_writable($log) )
$write_mode = ‘a’;

if( !$handle = fopen($log, $write_mode) )
return false;

if( fwrite($handle, $text. “\n”) == FALSE )
return false;

@fclose($handle);
}

Debugging Tools:

http://xdebug.org/
http://valgrind.org/
http://www.php-debugger.com/dbg/
http://pecl.php.net/package/apd
http://pear.php.net/package/Benchmark/
http://code.google.com/p/webgrind/

We can’t imagine processes without Log File 😉