2016-07-06 74 views
0

我这里休息服务器控制器笨调用REST服务器使用的file_get_contents

<?php 

require(APPPATH.'libraries/REST_Controller.php'); 

class Server extends REST_Controller { 

    public function __construct(){ 
     parent::__construct(); 
      date_default_timezone_set("Asia/Jakarta"); 
      $this->load->model('server_model'); 
    } 


    function customer_get() 
    { 
     if($this->get('id')){ 
      $users = $this->server_model->get_customer_by_id($this->get('id')); 
     }else{ 
      $users = $this->server_model->get_customer_all(); 
     } 

     if($users){ 
      $this->response($users); 
     }else{ 
      $this->response(array('error' => 'Data tidak ditemukan!')); 
     } 
    } 


    public function customer_post() 
    { 
     $data = array(
        'ktm'=>$this->post('ktm'), 
        'ktm_image'=>$this->post('ktm_image'), 
        'expired_ktm'=>$this->post('expired_ktm'), 
        'sim'=>$this->post('sim'), 
        'hp'=>$this->post('hp'), 
        'email'=>$this->post('email'), 
        'password'=>md5($this->post('password')), 
        'fakultas'=>$this->post('fakultas'), 
        'jurusan'=>$this->post('jurusan'), 
        'alamat'=>$this->post('alamat'), 
        'tanggal_lahir'=>$this->post('tanggal_lahir') 
       ); 

     $query = $this->server_model->create_customer($data); 

      if($query){ 
       $this->response($query); 
      }else{ 
       $this->response(array('error' => 'Tambah data gagal!')); 
      } 
    } 


    public function customer_put() 
    { 
     $id = $this->put('id'); 
     $data = array(
        'ktm'=>$this->put('ktm'), 
        'ktm_image'=>$this->put('ktm_image'), 
        'expired_ktm'=>$this->put('expired_ktm'), 
        'sim'=>$this->put('sim'), 
        'hp'=>$this->put('hp'), 
        'email'=>$this->put('email'), 
        //'password'=>$this->put('password'), 
        'fakultas'=>$this->put('fakultas'), 
        'jurusan'=>$this->put('jurusan'), 
        'alamat'=>$this->put('alamat'), 
        'tanggal_lahir'=>$this->put('tanggal_lahir') 
       ); 

     $query=$this->server_model->update_customer($data,$id); 

      if($query){ 
       $this->response($query); 
      }else{ 
       $this->response(array('error' => 'Edit data gagal!')); 
      } 
    } 


    public function customer_delete(){ 
     $id = $this->delete('id'); 

     $query = $this->server_model->delete_customer($id); 

      if($query){ 
       $this->response($query); 
      }else{ 
       $this->response(array('error' => 'Hapus data gagal!'), 404); 
      } 
    } 

} 

如何调用每一个方法像GET,POST,放,删除,并从客户端发送的数据,但使用的file_get_contents其余服务器。我使用休息服务器库codeigniter。

我试过在codeigniter中使用rest客户端库,它完全有效,但我现在需要使用file_get_contents,但我不知道如何调用它,特别是发送数据以便发布,放置和删除。

谢谢。

回答

0

必须为呼叫创建上下文,像这样:

<?php 
// Create a stream 
$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" . 
       "Cookie: foo=bar\r\n" 
) 
); 

$context = stream_context_create($opts); 

// Open the file using the HTTP headers set above 
$file = file_get_contents('http://www.example.com/', false, $context); 
?> 

参见php.net

file_get_contents()函数文档