コード例 #1
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // we'll store our data here
     $data = array();
     $pathArray = array();
     $pathURIArray = array();
     $pathURIArray = parse_url(strtolower($_SERVER["REQUEST_URI"]));
     // breaks the query string and path out
     // strip leading '/' TO DO - I was stripping the leading '/' - why was this? Win / Linux diff?
     // $path = $pathURIArray['path'];
     //	if (strlen($path) > 1 && substr($path,0,1) ==='/') {
     //		$path = substr($path, 1, strlen($path)-1);
     //	}
     // modified to use trim and to strip the first and final /
     // isset reqd for www.mydomain.com//news caused error
     if (isset($pathURIArray['path'])) {
         $path = trim($pathURIArray['path'], '/');
     } else {
         $path = "";
     }
     $pathArray = explode("/", $path);
     // error_log(print_r($pathArray,true));
     $return_obj->setPathArray($pathArray);
     // Get the URL variables / POST variables
     switch ($request_method) {
         // gets are easy...
         case 'get':
             // it's not as simple is the example I started from.  I'm looking for a ? in the URI as couldn't just use isset($_GET).
             //error_log($pathURI . ' - ' . strpos($pathURI, '?'));
             if (strpos($_SERVER["REQUEST_URI"], '?') !== FALSE) {
                 $data = $_GET;
                 //error_log (print_r($_GET,true));
                 //error_log (print_r($data,true));
             }
             //				else {
             //					$data = $pathURIArray;
             //				}
             break;
             // so are posts
             // TO DO TEST THIS
         // so are posts
         // TO DO TEST THIS
         case 'post':
             $data = $_POST;
             break;
             // here's the tricky bit...
             // TO DO TEST THIS
         // here's the tricky bit...
         // TO DO TEST THIS
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
     }
     // store the method
     $return_obj->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be other pieces to your requests)
     $return_obj->setRequestVars($data);
     return $return_obj;
 }