I have an application which goes like this -
Host A asks Host B if a file 开发者_JAVA技巧exists in Host B or not. So, A requests a REST API call to B. Now B should send back reply (it can be thru status codes) to A, after checking for the which is sent as the argument thru URL from A to B.
I am new to the REST API concept. I am clear of how A sends request to B but, can anyone tell me how B returns the value to A.
Thanks
I am sorry! I made a silly mistake, which after correcting gave me a result 200 - as expected.
Client A:
//Construct the REST call
$url = 'http://localhost/Receiver1.php?file=' . $filename;
//GET request with 'curl'
$ch = curl_init($url);
//Set Curl options
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
//Make the request
$response = curl_exec($ch);
//Get the status codes
$result = curl_getinfo($ch,CURLINFO_HTTP_CODE);
echo $result;
//Close the curl session
curl_close($ch);
switch($result)
{
case 200 :
return true;
break;
case 303 :
//my code
break;
}
Server B:
Now at the server side, a Receiver1.php script runs by taking the argument (filename) from the URL and prints if it exists in B or not. In this case, the server is my localhost. How does it get the arguments?
$filename = $_GET[file];
//Check if the file exists in the system
if(file_exists($filename.'.mpeg'))
{
$result = true;
}
else
{
$result = intval(false);
}
echo $result;
But in my localhost, what should I run? Should I check with http://localhost/Receiver1.php or what is the way? Please clarify this.
There is nothing special about REST responses. They are regular HTTP responses. A REST service could respond with HTML page, XML or JSPN document, or a simple string value. Either way is valid.
In your scenario, service A is a client to service B. A issues REST request (HTTP GET let's say),B accepts it and responds with putting a value in a response body (you choose the format), the A consumes the response body from B and does whatever it needs to do with it.
This is a very rough example:
A:
<?php
$response_from_b = file_get_contents('http://www.b.com/file/some-file-on-b.ext');
// do whatever you need with $response_from_b
?>
B:
<?php
$file_path = parse($_SERVER['REQUEST_URI']); // parse is your own function to get what you want form URI
$native_response = file_exists($file_path); // your own function
$response = decorate($native_response); // your own function
echo $response;
?>
精彩评论