Category: PHP

A new project – WordPress SQLite

WordPress runs pretty fine in MySQL. But what if I want to run it in SQLite or in PostgreSQL? No way!!

Last night I started a new project and I am converting wordpress DB-Core as well as the db schema to SQLite. It will be a great project if successful.

I hope I can release it within 3-5 days.

Long Live WordPress.

Zend is launching PHP5 Exam

Zend is launching PHP5 certification exam pretty soon. The beta is starting from this July 26 – Sunday to August 2. Today I received a mail from Dhwani Wahia from Zend Education Advisory board announcing the upcoming Exam.

But There is a surprise. Zend is giving away 25 Free beta exam voucher chosen from existing Zend Certified Engineers. Lets see the Result. Zend already published the curriculum for PHP5 exam.

Great

Storing Bangla Unicode Text in MySQl Database

Well, If you just set the collation as “utf8-general-ci” that wont do all the things for storing and retrieving bangla unicode texts properly. There are something more to do. Last night while working for a project I just found the solution.

You must add these two lines just after selecting the database, i.e mysql_select_db() function.

mysql_query(‘SET CHARACTER SET utf8’);
mysql_query(“SET SESSION collation_connection =’utf8_general_ci'”);

After executing these two statements MySQL will handle the rest.

Using Blog API's

Almost every standard compliant blogs support three blogging API which are “Blogger API” , “MovableType API” and “MetaWeblog API”. Among these three, MW (or MetaWeblog) is used more than other two. WordPress also supporst these three sets of API. If you are interesetd how to interact with these API’s – then take a look at the following example.

First lets discover which XMLRPC methods are suported by wordpress XMLRPC server. After digging a bit further we found that the following methods are supported

system.multicall
system.listMethods
system.getCapabilities
demo.addTwoNumbers
demo.sayHello
pingback.extensions.getPingbacks
pingback.ping
mt.publishPost
mt.getTrackbackPings
mt.supportedTextFilters
mt.supportedMethods
mt.setPostCategories
mt.getPostCategories
mt.getRecentPostTitles
mt.getCategoryList
metaWeblog.getUsersBlogs
metaWeblog.setTemplate
metaWeblog.getTemplate
metaWeblog.deletePost
metaWeblog.newMediaObject
metaWeblog.getCategories
metaWeblog.getRecentPosts
metaWeblog.getPost
metaWeblog.editPost
metaWeblog.newPost
blogger.deletePost
blogger.editPost
blogger.newPost
blogger.setTemplate
blogger.getTemplate
blogger.getRecentPosts
blogger.getPost
blogger.getUserInfo
blogger.getUsersBlogs

so to make a new post using metaWeblog.newPost method lets take a look at the following example.

<?php
	include("xmlrpc.inc.php");
	$c = new xmlrpc_client("/wp/xmlrpc.php", "localhost", 80);

	$content['title']="XMLRPC Post";
	$content['description']="Some content posted using MetaWeblog API";
	$content['categories'] = array("frontpage");
	$x = new xmlrpcmsg("metaWeblog.newPost",
	                    array(php_xmlrpc_encode("1"),
                        php_xmlrpc_encode("admin"),
                        php_xmlrpc_encode("root"),
                        php_xmlrpc_encode($content),
                        php_xmlrpc_encode("1")));

	$c->return_type = 'phpvals';
	$r =$c->send($x);
	if ($r->errno=="0")
	echo "Successfully Posted";
	else {
		echo "There are some error";
		print_r($r);
              }
?>

Thats it!!

FeedPHP is going to be the largest PHP News Source

php-feeds.gif

FeedPHP is a free RSS aggregator which collects feed, parse and display the titles with a link back to the original source. This is one top news source for PHP and relevant technologies. This website is currently displaying news feeds from different fields like PHP, PHP Blogs, MySQL, PostgreSQL, Ruby, Java and others. No doubt, this is going to be the largest PHP news source in near future.

The whole site is a single page with mod_rewrite tricks. For performance, the contents are cached for several minutes, otherwise my site will be convicted for consuming excess bandwidth from those sites. Moreover, the site will be extremely slow for consuming remote contents frequently. Output buffering was used to serve the content quickly.

ZipLib2 – The Top rated class in phpclasses.org, compression category

I developed ZipLib2, a wrapper over the original Ziplib class, one year ago to create zipped archives using PHP. During this time It was in the top position in compression category in phpclasses. After arrival of dUnzip and some other very good classes, ZipLib2 losses its position and went to third/fourth place. After that I modified the code a little bit and add a huge documentation which was not present in previous release.

Now ZipLib2 is the Top user rated class in Compression Category with 82.4% rating. Thats some good result.

Visit : http://www.phpclasses.org/browse/package/2088.html

Caching – Save visitors time and Balance the load

Caching is a major process of speeding up your application. By caching the output of a potentially large scale operation and displaying that output later from that cache saves a lot of time. First if you have cached output of a large scale operation, just check whether the cache is out-of-date or not. If it succeeds all the parameters, display the cache which saves that time consuming procedures. These large scale operation may include fetching content from an remote source using CURL or any stream wrapper, or a database operation, or even a time consuming graph/chart generation.

There are several types of caching, like opcode caching, output caching etc. Compiler level caching are out of scope in this article. We are simply talking about the output caching. In this step PEAR::Cache or PEAR::Cache_Lite may save really a great amount of time. Lets take a look how.

In the following example we are using PEAR::Cache. You can also use Cache_Lite which functions great by sacrificing a bit performance.

<?
include("Cache/Output.php");

$cache = new Cache_Output("file");
if (!$cache->start($cacheid, "samplegroup"))
{
	//perform large scale operation
        echo "Hello";
	$cache->end();
}
else
{
	echo "Printing from Cache";
}

//display the cached content
echo $cache->get($cacheid);
?>

First time, the output is generated as usually. But from next time the output is displayed from cached bypassing the large scale operation.

You can flush content of any cached group by accessing it’s flush() method which looks like below

$cache->flush( “samplegroup”);

Thats it.

Difference between $_XXX and $HTTP_XXX_VARS

Almost every PHP developer is familiar with $_POST and $HTTP_POST_VARS arrays. Can you tell any significant difference between them? Well, you may think that there is no difference except that the former one was supported after PHP 4.1.0. But the truth is they have one very significant difference between them.

$_POST is a superglobal array and it is also known as an autoglobal array. That makes it available under any scope. Whether you are calling them inside a class, or function you need to declare it as global $_POST.

The $HTTP_POST_VARS is not a superglobal array in truth. If you want to access it inside any function, you must declare it as global. Otherwise it will be considered as a normal array in current function scope which is not what you actually want.

$HTTP_POST_VARS will not be considered as a superglobal array until register_global is turned on.

The same difference exists between their peers like $_GET and $HTTP_GET_VARS, $_FILES and $HTTP_POST_FILES, $_COOKIE and $HTTP_COOKIE_VARS, $_SESSION and $HTTP_SESSION_VARS.

If you are not aware of these difference, you script may fall into severe compatibility problem and you may spend many hours to debug it.

use CURL with HTTP_RANGE header

Hi
Last night I was working with curl extension to see if there is anything so that I can download remote contents with from specific range. For example I want to download a page from offset 0 to 999, that means first 1000 byte. Or a zip file from offset 1000 to 3000, which means 2000 byte starting from 1000 byte offset. If You can do this, you can easily develop remote down loader with resume support.

Finally I have found that there is a option named CURLOPT_RANGE by which you can specify HTTP_RANGE. Lets take a look at the following code which download first 1000 byte from http://www.phpxperts.net/SmartyCheatSheet.pdf file.

<?
$curl = curl_init("http://www.phpxperts.net/SmartyCheatSheet.pdf");
curl_setopt($curl, CURLOPT_RANGE, "0-999");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
echo $content;
?>

well, this is the process. I hope many of you will find this useful.