2013-04-01 246 views
1

我有Java REST API Server与ServerResource在一些文档上创建哈希码,但我真的不知道,如何构建PHP休息客户端,有人可以帮忙吗? (一些例子或教程如何做到这一点?)我没有使用PHP的经验,所以我会很感激帮助。如何通过PHP连接到我的本地主机?如何将一些字符串发布到Java中的哈希资源?PHP REST客户端

我已经从phphttpclient.com/下载了剩余客户端,所以我选择了一些文件,我有该文件的内容..现在可以将它发送到我的Java REST API(localhost:port/hash)吗?

+1

您可以使用卷曲 - http://php.net/manual/en/book.curl.php – Gntem

+0

你居然主办的PHP文件?你确实有一个Web服务器或其他东西在运行?能够从磁盘执行一个PHP文件是分开执行它使用url –

回答

1

我想你只需要一个非常简单的REST客户端在PHP中发送请求并接收响应以便与REST服务器进行通信。 REST客户端可帮助您专注于发送数据,而不是关注如何发布或获取数据等较低级别的事情。 您可以使用this或其他。对于更简单的,如果您使用像Zend这样的框架,您应该尝试HTTPFUL,尝试this

+0

加一个HTTPFul http://phphttpclient.com/ –

0

这不是一个原始的方式来建立在PHP的休息API,这将帮助你启动它。

这里有主要的你需要在php中创建休息类, 我已经给出了PHP中的主休息类。

<?php 
class REST { 
    // initialize the database connection 
    const DB_SERVER = "localhost"; 
    const DB_USER = "root"; 
    const DB_PASSWORD = "pass"; 
    const DB = "vtms"; 

    // set the conntent type and object to the api 
    public $_allow = array(); 
    public $_content_type = "application/json"; 
    public $_request = array(); 

    private $_method = "";  
    private $_code = 200; 

    //construct the input for the rest call 
    public function __construct(){ 
     $this->inputs(); 
    } 

    //Set reference to the webserver 
    public function get_referer(){ 
     return $_SERVER['HTTP_REFERER']; 
    } 

    // give the status of the rest operation 
    public function response($data,$status){ 
     $this->_code = ($status)?$status:200; 
     $this->set_headers(); 
     echo $data; 
     exit; 
    } 
    // For a list of http codes checkout http://en.wikipedia.org/wiki/List_of_HTTP_status_codes 
    private function get_status_message(){ 
     $status = array(
        200 => 'OK', 
        201 => 'Created', 
        204 => 'No Content', 
        404 => 'Not Found', 
        406 => 'Not Acceptable'); 
     return ($status[$this->_code])?$status[$this->_code]:$status[500]; 
    } 

    public function get_request_method(){ 
     return $_SERVER['REQUEST_METHOD']; 
    } 
    // define curd operation of the api 
    private function inputs(){ 
     switch($this->get_request_method()){ 
      case "POST": 
       $this->_request = $this->cleanInputs($_POST); 
       break; 
      case "GET": 
      case "DELETE": 
       $this->_request = $this->cleanInputs($_GET); 
       break; 
      case "PUT": 
       parse_str(file_get_contents("php://input"),$this->_request); 
       $this->_request = $this->cleanInputs($this->_request); 
       break; 
      default: 
       $this->response('',406); 
       break; 
     } 
    }  

    private function cleanInputs($data){ 
     $clean_input = array(); 
     if(is_array($data)){ 
      foreach($data as $k => $v){ 
       $clean_input[$k] = $this->cleanInputs($v); 
      } 
     }else{ 
      if(get_magic_quotes_gpc()){ 
       $data = trim(stripslashes($data)); 
      } 
      $data = strip_tags($data); 
      $clean_input = trim($data); 
     } 
     return $clean_input; 
    }  
    // set headers to the content 
    private function set_headers(){ 
     header("HTTP/1.1 ".$this->_code." ".$this->get_status_message()); 
     header("Content-Type:".$this->_content_type); 
    } 
} 
?> 

如果你复制粘贴此代码没有你几乎做任何错误,现在创建一个类的对象扩展到这个REST API,例如。

<?php 

require_once(“Rest.inc.php”);

类API扩展REST {

public $data = ""; 
private $db = NULL; 
private $mysqli = NULL; 

public function __construct() { 
    parent::__construct(); // Init parent contructor 
    $this->dbConnect();  // Initiate Database connection 
} 

/* 
* Connect to Database 
*/ 

private function dbConnect() { 
    $this->mysqli = new mysqli(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD, self::DB); 
} 

/* 
* Dynmically call the method based on the query string 
*/ 

public function processApi() { 
    $func = strtolower(trim(str_replace("/", "", $_REQUEST['x']))); 
    if ((int) method_exists($this, $func) > 0) { 
     $this->$func(); 
    } else { 
     $this->response('', 404); 
    } // If the method not exist with in this class "Page not found". 
} 
    private function getAllstudent() { 
    if ($this->get_request_method() != "GET") { 
     $this->response('', 406); 
    } 

    $query = "SELECT * from students"; 
    $r = $this->mysqli->query($query) or die($this->mysqli->error . __LINE__); 

    if ($r->num_rows > 0) { 
     $result = array(); 
     while ($row = $r->fetch_assoc()) { 
      $result[] = $row; 
     } 
     $this->response($this->json($result), 200); // send user details 
    } 
    $this->response('', 204); // If no records "No Content" status 
} 

    private function json($data){ 
     if(is_array($data)){ 
      return json_encode($data); 
     } 
    } 


} 
$api = new API; 
$api->processApi(); 

只需打开.htaccess文件,并输入验证码

<IfModule mod_rewrite.c> 

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-s 
    RewriteRule ^([a-zA-Z0-9-]+)/studentapi/method/ student.php?x=$1 [QSA,NC,L] 

</IfModule> 

创建表调用学生和输入一些值给它,而现在你打开浏览器,键入这个URL, http://localhost/yourserviceFodername/getAllStudent/studentapi/method/student

振作起来......