Home > PHP > Using cURL for XMLRPC calls

Using cURL for XMLRPC calls

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

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

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

Shoaib Mir

Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: