How can we use CURL to get data from web service

July 22, 2008

I made an example web service using CURL (Client URL Library Functions). By using this example you can create your own (News, weather or any other web service). Here this example, we have two files form.php for client server and curl_response.php for web service server. The curl_response.php file must be hosted in a separate server like and any client server ( e.g. form.php ) just by using the form.php with username and accesskey can access the web service. You can run this in your localhost also, just open the php.ini file and active the CURL by removing “;” form ;extension=php_curl.dll and save the files and restart your WAMP/XAMP server.

form.php holds the following codes:

* CURL (Client URL Library Functions) TEST
* This is an example, how we can use CURL to get data from web service
* form.php is the Client form for getting data from web service
* MD. Nazmul Basher
<div align=”center”>
<fieldset style=”width:200px; padding:3px; margin:10px;”>
<legend>CURL TEST</legend>
<form name=”reg” action=”#” method=”post”>
<label for=”name”>User Name:&nbsp;</label>
<input type=”text” name=”username” id=”username”/>
<p><input type=”submit” name=”submit” value=”Submit”/></p>

// Give the URL of your web service, e.g
/* Processing the request with url encoding */
$postfields = array();
$postfields[] = array(“username”, $_POST[‘username’]);
$postfields[] = array(“accesskey”, “k9utr45u56798”);

foreach($postfields as $subarray) {
list($foo, $bar) = $subarray;
$bar = urlencode($bar);
$postedfields[] = “$foo=$bar”;
$urlstring = join(“:”, $postedfields);
$urlstring = ereg_replace(“:”, “&”, $urlstring);

/* Send this request by CURL*/
$ch = curl_init($url); // Initialize a CURL session
curl_setopt($ch, CURLOPT_HEADER, 0); // options for a CURL transfer
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlstring);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch); // Perform a CURL session
curl_close($ch); // Close a CURL session

echo “<div align=’center’>”.$data.”</div>”; // Display the output


curl_response.php holds the following codes:

* An Example Web Service for testing, I made this as example news services
* you can create this for news, weather, zipcode, etc web services
* This class holds, How a Web Service can be used.
* @package Web Service
* @category Library
* @author Md. Nazmul Basher (
* @Web

class webService
var $userinfo;
var $data;

function webService() {
/* This is an example so I used array, you can get data for database*/
$this->data=array(“title”=>”World NEWS”,”fulltext”=>”<p>Lorem ipsum dolor sit amet consectetuer Nam convallis Fusce facilisis est. Nunc Morbi Phasellus habitant eget at nunc Sed nibh facilisis ornare. Nulla at facilisis eget et laoreet tincidunt ut Nulla tristique enim. Orci aliquet tincidunt congue tortor cursus nonummy non nunc metus et. Eleifend nec Aenean et iaculis dui condimentum nibh Quisque elit congue. Purus et consequat ut.</p><p>Curabitur nibh sed In quis Cum dolor fringilla morbi tristique gravida. Morbi eu pede Duis fames id lacus hendrerit consequat hendrerit ligula. Amet sapien nulla montes pede id Nulla sem et faucibus justo. Quisque odio nunc lorem volutpat a fringilla ligula wisi et augue. Metus feugiat Donec massa Morbi ac.</p><p>Dictum sem porta cursus adipiscing nisl malesuada velit Aenean risus interdum. Sem justo auctor platea vitae volutpat ac sem ipsum est vitae. Leo tortor massa ac tincidunt cursus dapibus adipiscing id lacus eget. Et vel semper ac eros eget faucibus Curabitur augue leo dis. Faucibus accumsan et adipiscing elit orci condimentum Lorem dui ut nunc. </p>”);

*Web Service Response Function
*@param $uname : User Name
$key : Access Key for that user to access the resources for the server
*@return display the output

function webResponse($uname,$key) {

if((!empty($uname)) && (isset($key))){
if(($this->userinfo[username]==$uname) && ($this->userinfo[accesskey]==$key)){
echo “<strong>”.$this->data[title].”</strong>”.$this->data[fulltext];
else{ echo “<strong>Error: you are not authorised</strong>”; }
else{ echo “<strong>Error: Sorry Try Again</strong>”; }

} // End Class

$service = new webService();
// Call the Response function