brrocks28
6/20/2016 - 1:32 PM

CONSUMING WEB-SERVICES json xml

CONSUMING WEB-SERVICES json xml

CONSUMING WEBSERVICES

JSON SAMPLE API: 
http://ip.jsontest.com/
http://api.geonames.org/weatherJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo

xml sample api:
http://api.geonames.org/cities?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
http://www.thomas-bayer.com/sqlrest/CUSTOMER

1)Simple way (using file_get_contents)

--------------------------------------------------------------------------
Json:

$response = file_get_contents('http://api.geonames.org/weatherJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo');

//if $response is JSON, use json_decode to turn it into php array:

$response = json_decode($response);

dsm($response);


-------------------------------------------
xml:


$response = file_get_contents('http://www.thomas-bayer.com/sqlrest/CUSTOMER');

//if $response is XML, use simple_xml class:

$xml = simplexml_load_string($response);

$json = json_encode($xml);

$array = json_decode($json,TRUE);

dsm($array);




create custom web services :
http://pingv.com/blog/an-introduction-drupal-7-restful-services