jcadima
4/28/2016 - 6:10 AM

Creating a Basic Web Service

Creating a Basic Web Service

<?php

/*
base.txt contents:
Honda,
Toyota,
Nissan
*/


// this could come from an internal or external service
$curl = curl_init("http://mvc.net/base.txt") ;

curl_set_opt ( $curl, CURLOPT_RETURNTRANSFER, true ) ;

$answer = curl_exec ($curl) ;

$info = curl_getinfo ( $curl ) ;

// check status code
if ( $info['http_code']  == 200 ) {
	// get the data , split by commnas, and process
	$data = explode(",", $answer ) ;
	echo "Cars in inventory:" ;
	foreach ($data as $car) {
		echo "-> " . $car . '<br>' ;
	}
}
else {
	echo "Error:  " . curl_error($curl) ;
}



/*
OUTPUT:
Cars in inventory:
Honda
Toyota
Nissan
*/