Friday 7 December 2012

Access asmx webservice from PHP using CURL


Consuming .asmx webservices is not so much complex nowadays.We can use CURL for posting variables to an asmx webservice. You can get the output in a few steps. Here are the details:

1)      Assign URL and variables
2)      Post using curl
3)      Prints the Output

Here is the code.

<?PHP
 
//web service URL and variables
 
$url = 'http://172.16.0.35/SoleTraderTaxiLocal/SoleTraderServices.asmx/SearchUsers'; 
 
$fields = array('AuthenKey'=>'172839',
    
'Criteria'=>'userfirstname',

'SearchText'=>'j',

'UserTypeiCode'=>'2',

); 
 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
 
rtrim($fields_string,'&');
 
// Posting with CURL 
 
$ch = curl_init();
 
curl_setopt($ch,CURLOPT_URL,$url);
 
curl_setopt($ch,CURLOPT_POST,count($fields));
 
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$result = curl_exec($ch);
 
// Output to an xml file 
 
file_put_contents('content123.xml',$result);
 
curl_close($ch);
 
?>


Hope this helps!



2 comments:

  1. • I very much enjoyed this article. Nice article thanks for given this information. I hope it useful to many Peopledata since Online course

    ReplyDelete