SOAP to JSON using PHP

For anyone that wants to translate output from a SOAP webservice using WLSD to JSON… Here is an example script that shows you the basics 🙂

This is a working example.

You need at least PHP 5.1 for this example to work!

<?php
// Setup and connect the soap client object
try{
	$sc = @new SoapClient('http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL');
} catch (Exception $e) {
	echo $e->getMessage();
}

// Fetch the uri parameter //
if(isset($_GET['c']) && !empty($_GET['c'])){
	$country['name'] = addslashes(ucwords($_GET['c']));
}else{
	$country['name'] = 'FALSE';
}

// Query the soap webservice
$result = $sc->CountryISOCode(array('sCountryName' => $country['name']));
$result = $sc->FullCountryInfo( array(sCountryISOCode => $result->CountryISOCodeResult));

// Output the Json code
echo json_encode($result->FullCountryInfoResult);

print('
<h2>Functions described in wsdl</h2>
<br\>');
foreach($sc->__getFunctions() as $key => $value){
	echo "$key => $value
";
}

print('
<h2>Types described in wsdl</h2>
<br\>');
foreach($sc->__getTypes() as $key => $value){
	echo "$key => $value
";
}

?>

Goodluck , and happy scripting! 🙂

Advertisements

2 thoughts on “SOAP to JSON using PHP

  1. Although this article helped me greatly I have never seen this before $country{‘name’} = ‘FALSE’;

    where can i read about using {} instead of [] for array keys?

  2. You cant, it was a typo that I didn’t pay much attention to.
    (was prob. tired, sorry for that).

    Even though it works (php weakly typed), you should use the [”] notation where possible being best practice and all.

    Corrected the code accordingly 🙂

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 )

w

Connecting to %s