Header Ads

Simple Web Service Using PHP + MYQSQL + JSON + CURL. Part 3 - Client

1.     Initialize Curl.
$cSession = curl_init();

2.     URL to web service service.
$url = http://localhost/webservice/server.php;
$request = "allprices";
$webserviceserver = $url . "?request=" . $request;
curl_setopt($cSession,CURLOPT_URL,$webserviceserver);

//Tell php curl that we want the data returned to us instead of being displayed curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
       
3.     Returned result from curl.
//Execute curl and put the output in $retresult
$retresult = curl_exec($cSession);

4.     Convert Json into arrays.
$result = json_decode($retresult);
var_dump($result);

5.     Final code.
<?php

$cSession = curl_init();

$url = http://localhost/webservice/server.php;

$request = "allprices";

$webserviceserver = $url . "?request=" . $request;

curl_setopt($cSession,CURLOPT_URL,$webserviceserver);

//Tell php curl that we want the data returned to us instead of being displayed curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);

curl_setopt($cSession,CURLOPT_HEADER, false);

$retresult=curl_exec($cSession);

curl_close($cSession);

$result = json_decode($retresult);

var_dump($result);
?>

6.     Save this file as client.php

7.     Run this file in the browser.

No comments