How to call webservice in php?

I have called web services in php by using curl function. In below I have given  one example.  I have sent XML Request by using curl function.Because My web services return data on XML Format.

$xml_data = ''; //pass parameter using XML request provide By webservices
$postData = array(     // You can pass parameter using Array and pass in postfields.

    'authId'=>'',
    'siteId'=>'',
    'countryId'=>'',
    'regionId'=>'',
	'weekType'=>'',
	'startDate'=>''
);

$URL = "http://www.yourwebservices.com/";

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);

After that Load XML data using DOM Document in php

$doc = new DOMDocument();
$doc->loadXML($output);

Finally retrieve data by using XML Tag Name.

 //count is Tag name of XML.
echo $count = $doc->getElementsByTagName('count')->item(0)->nodeValue;
Advertisements

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