Monday, January 7, 2013

send request and get response using php json and curl client and server page

stp-1 create you client page which can send the request to server page
like sendclientrequest.php and paste below code

<?php
$url = "http://127.0.0.1/test/ReceiveRequestReturnResponse.php";
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'xmlstr='.$xmlStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = curl_exec($ch);
$row=json_decode($data,true);
foreach($row as $fields){
echo $fields;
}
?>
---------------receive request and send response using JSON AND CLIENT curl-----------------------
create ReceiveRequestReturnResponse.php get request and return respose in json curl
<?php
$row=array('id'=>2,'name'=>'bikash','address'=>'delhi');
echo json_encode($row);
?>

No comments:

Post a Comment