Archive

Archive for the ‘PHP’ Category

xAjax and asynchronous updates

March 12, 2009 Leave a comment

Most of the libraries available for Ajax requires you to write your own functions in Javascript to process callbacks from the asynchronous requests in order to update the data but xajax gives you its own xajaxResponse object through which you can handle all within PHP. xajaxResponse object is here used to create XML which is returned to the application and that is then parsed by xajax message pump that results in updating the state of the application.

xajaxResponse gives a number of nice to use features like:

Assign –> This sets the specified attribute of an element in your page

Append –> Is used to append data to the end of the specified attribute of an element in your page

Prepend –> Prepends data to the beginning of the specified attribute of an element in your page

Replace –> Which searches for and replaces data in the specified attribute of an element in your page

Script –> This runs the supplied JavaScript code

Alert –> Shows an alert box with the supplied message text


Shoaib Mir
shoaibmir[@]gmail.com

Advertisements
Categories: PHP Tags: , , ,

A quick howto of Ajax with PHP

March 11, 2009 Leave a comment

I have been using Ajax for one of our main applications with PHP. We are using the Xajax PHP class libary for that purpose which gives a very easy to use interface.  I thought of sharing a very simple howto for the xajax library in case anyone wants to have a quick look at it…

//Include the xajax class library
require_once(“xajax_core/xajax.inc.php”);

//Initialize a new Xajax object
$xajax = new xajax();

//Register the name of function that you will be calling from Xajax
$xajax->registerFunction(“myFunction”);

//Now the function that uses xajaxResponse object for returning XML
function test_func($arg)
{
$display_val = “Passed to function: “.$arg;
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id=”v_ElementId” to whatever the new content is
$objResponse->assign(“v_ElementId”,”innerHTML”, $display_val);

//return the  xajaxResponse object
return $objResponse;
}

//Before your script sends any output, use the xajax handle for any requests
$xajax->processRequest();

//Tells ajax to generate the appropriate Javascript
<?php $xajax->printJavascript(); ?>

//Now you can use the function in your HTML code like this
<div id=”v_ElementId”></div>
<button onclick=”xajax_myFunction(‘Hello World!”);”>


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: , ,

Using cURL for XMLRPC calls

March 10, 2009 Leave a comment

cURL is a project name which came from  ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious it deals with URLs. cURL has basically two products, a command line and a library. We are more concerned with the library as that is used with PHP for cURL calls. LibCurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE, LDAP and LDAPS. libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy tunneling and more…

So before I go with details on the sample program, make sure you got cURL and xmlrpc modules installed for PHP. This can be easily done by compiling the cURL and xmlrpc extensions from PHP source and linking it though the php.ini file. Once that is done make sure
you can see the modules installed using phpinfo function.

Once all looks good when cURL and xmlrpc installations have been verified, you can use the following code in PHP to do the xmlrpc calls…

//Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request($method, array($user, $pass));
$req = curl_init($service_url);

// Using the cURL extension to send it off,  first creating a custom header block
$headers = array();
array_push($headers,”Content-Type: text/xml”);
array_push($headers,”Content-Length: “.strlen($request));
array_push($headers,”\r\n”);

//URL to post to
curl_setopt($req, CURLOPT_URL, $service_url);

//Setting options for a secure SSL based xmlrpc server
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($req, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt( $req, CURLOPT_CUSTOMREQUEST, ‘POST’ );
curl_setopt($req, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($req, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $req, CURLOPT_POSTFIELDS, $request );

//Finally run
$response = curl_exec($req);

//Close the cURL connection
curl_close($req);

//Decoding the response to be displayed
echo xmlrpc_decode($response);


Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: , ,

PHP and Internationalization support

February 20, 2009 Leave a comment

For the last almost three months I have been doing some PHP programming and its been fun using it 🙂

The project I have been working on is almost complete from the features side that we needed to support but now the issue which just came in was having language support and Internationalization the whole GUI. That actually means having a “Select Language” option on the login screen and then customizing the whole GUI according to that. I have done similar kind of support before in C but at that time I was using a very nice GkyeFile parser from Glibc which had proper support for locale, but being new at PHP I had no idea where to start with and as I started looking for a solution, it ended up being a very simple one. Here is what can be done if someone wants to do the same thing simply in PHP….

– Have a global array called $language and then assign all your GUI labels and error messages like this:

$language[‘btnlbl’] = ‘Button’;
$language[‘Err001’] = ‘Error executing function’;

And put this array in a PHP file (where each language like english, french, german etc etc have a specific {langname}.php file)

– Now we need to use the above mentioned language PHP files…

We kept English as the master language so thats the default, this is how it can be handled:

//If no language is set use English
if (!isset($configuration[‘defaultlanguage’])) $configuration[‘default_language’] = ‘english’;

//Setting up the array and setting all values from the PHP file
$language = array();
require_once(‘./language/english.php’);

And if user has select a specific language on the login screen use the following:

if (isset($_language)) {
include(“./language/{$_language}.php”);
$_SESSION[‘weblang’] = $_language;
}

Now all I had to do was use english.php through out the code and then send the other {language}.php to the translators… and we ended up having a proper internationalization support.

Shoaib Mir
shoaibmir[@]gmail.com

Categories: PHP Tags: ,

xmlrpc over https implementation

January 8, 2009 Leave a comment

On a project we were using SSH2 module of PHP, but it was turning out to be a total mess due to compatibility issues on different operating and for the reason that it cant be built on all the operating systems easily. This made us to think about other alternatives but there are not many option in PHP where you can get much more control when using SSH.

The solution was to write our own Agent that could perform all kinds of functions we were using from the SSH library of PHP. The two functions we were using the most were:

– SSH function to run a program or service on a remote machine
– SSH function to copy a file to a remote

When writing our own agent we made a choice to go with xmlrpc as it is a pretty simple library and it will not be months of work using xmlrpc to implement our own server.

Now when looking for xmlrpc libararies for C/C++ you do not have a lot of choices available when you want to go for a HTTPS standalone server (because we can not install web server on an Agent machine). So in the end we went with “libiqxmlrpc” which is a C++ based library and very easy to use.

While implementing the server functionality the only problem that I didnt like was when ever you run a new process through libiqxmlrpc based server the process basically attaches it self to the sane port the server was running so even if the server quits, the server port is still blocked. For that we had to modify the library code in way to get the file descriptor for server socket and then close that socket using the file descriptor ID in the Server code when forking a child to run the process.

Other then this libiqxmlrpc library has been flawless for me and it integrated very nicely with PHP side as well using cURL that supports HTTPS server through its OpenSSL support.

Anyone doing xmlrpc over HTTPS, I will highly recommend libiqxmlrpc.

-Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++, PHP Tags: ,