How can we use CURL to get data from web service

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 http://example.com/curl_response.php and any client server ( e.g. http://nazmul.com/ 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
* nazmul_basher@yahoo.com
* http://nazmulb.wordpress.com
*
–>
<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>
</form>

</fieldset>
</div>
<?php
// Give the URL of your web service, e.g http://example.com/curl_response.php
$url=”http://localhost/curl_response.php&#8221;;
if(isset($_POST[‘submit’])){
/* 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:

<?php
/**
*
* 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 (nazmul_basher@yahoo.com)
* @Web http://nazmulb.wordpress.com
*
*/

class webService
{
var $userinfo;
var $data;

function webService() {
/* This is an example so I used array, you can get data for database*/
$this->userinfo=array(“username”=>”nazmul”,”accesskey”=>”k9utr45u56798″);
$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
$service->webResponse($_POST[username],$_POST[accesskey]);
?>

About these ads

2 Responses to How can we use CURL to get data from web service

  1. saeed ahmed says:

    i’m trying to study on curl, spider, seo. but i can’t find any good reference. actually i want to make a search engine. i think works on this topics. it will be very kind of you, if you giving me some reference how i started or some advice. help me about curl by giving some book name or reference if you can.

  2. nazmulb says:

    Mr. saeed ahmed , you can learn CURL from http://www.php.net/curl

    Thanks

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: