1

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

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

4

2 回答 2

1

我认为您只需要一个非常简单的 php REST 客户端来发送请求和接收响应,以便与 REST 服务器进行通信。REST 客户端帮助您专注于要发送的数据,而不是关心较低级别的事情,例如如何发布或获取数据。对于更简单的,如果你使用像 Zend 这样的框架,你应该尝试 HTTPFUL,试试 Zend/Rest/Client

于 2013-07-13T06:08:09.597 回答
0

这不是在 php 中构建 rest api 的原始方法,这将帮助您启动它。

这里有main你只需要在php中创建rest类,我已经在php中给出了主要的rest类。

<?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);
    }
}   
?>

如果您复制并粘贴此代码而几乎没有任何错误,那么现在创建一个对象类扩展至此 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>

创建表调用 student 并向其输入一些值,现在打开浏览器并输入此 URL http://localhost/yourserviceFodername/getAllStudent/studentapi/method/student

开心点...

于 2015-09-22T11:21:52.477 回答