I have an application, where client A sends a REST request to Server B with filename as an argument in the URL and the Server B should respond with printing if the file exists in its disk or no.
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
$开发者_StackOverflowresponse = 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.
The server can parse the client request string to break up the resource. The trick here is to redirect all URLs to be handled by one PHP script. A typical situation would be to create an .htaccess file to redirect all URLs. I've stolen this .htaccess from Recess Framework as an example.
.htaccess:
Options FollowSymLinks
RewriteEngine On
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ bootstrap.php [QSA,L]
Place that file in your web directory. If you have a properly configured mod_rewrite(please Google for instructions if necessary), all web requests will be forwarded to the bootstrap.php file. Now in your bootstrap.php file, put the following code:
echo "Processing: " . $_SERVER['REQUEST_URI']."<br>";
$resources = explode('/',$_SERVER['REQUEST_URI']);
var_dump($resources);
/**
* $resource now contains the pieces of the client's requested URL.
* If we know what resource the client wants, we pass the parameter
* on to that resource's handler.
*/
if( $resources[1] == 'files' ) {
echo handleFiles($resources[2]);
} else {
echo "Unknown resource!<br>";
}
function handleFiles($name) {
if(file_exists($filename.'.mpeg')) {
$result = true;
} else {
$result = intval(false);
}
return $result;
}
What we are doing here is breaking the URL up into pieces. For example, the client may want to access http://localhost/files/mySoundFile. We break the URL into pieces, "files" and "mySoundFile". We can look at the pieces to determine how to process the request.
This is a very brief example of how to breakup a URL in PHP. I highly recommend using a framework (like http://www.recessframework.com or Zend's REST). Also search for REST php on Google to get some good pointers(http://rest.elkstein.org/2008/02/using-rest-in-php.html). Good luck!.
精彩评论