Crear cakephp rest client

Para probar si funcionan  bien los webservices creados vamos a crear una web que los ‘consuma’. En cualquier cakephp (puede ser el mismo) crearemos un controlador ClienteController:

 <?php

App::uses('AppController', 'Controller');
App::uses('HttpSocket', 'Network/Http');

class ClienteController extends AppController {

 public $components = array('Security', 'RequestHandler');
//Aquí pondréis la url de vuestra web
 public $url = "http://localhost/cakeempresa/";

 public function index() {
 
 }

 public function rest_index() {
 $link = $this->url . 'rest_productos.json';

 $data = null;
 $httpSocket = new HttpSocket();
 $response = $httpSocket->get($link, $data);
 $this->set('response_code', $response->code);
 $this->set('response_body', $response->body);

 $this->render('/Cliente/respuesta');
 }

 public function rest_view($id) {

 $link = $this->url . 'rest_productos/' . $id . '.json';

 $data = null;
 $httpSocket = new HttpSocket();
 $response = $httpSocket->get($link, $data);
 $this->set('response_code', $response->code);
 $this->set('response_body', $response->body);

 $this->render('/Cliente/respuesta');
 }

 public function rest_add() {

 $link = $this->url . "rest_productos.json";
 $data = null;
 $httpSocket = new HttpSocket();
 $data['Producto']['precio'] = 74;
 $data['Producto']['proveedor_id'] = 1;
 $data['Producto']['referencia'] = 'Extra';
 $response = $httpSocket->post($link, $data);

 $this->set('response_code', $response->code);
 $this->set('response_body', $response->body);

 $this->render('/Cliente/respuesta');
 }

 public function rest_edit($id) {

 $link = $this->url . 'rest_productos/' . $id . '.json';
 $data = null;
 $httpSocket = new HttpSocket();
 $data['Producto']['precio'] = 27;
 $data['Producto']['referencia'] = 'Actualizada referencia';
 $response = $httpSocket->put($link, $data);
 $this->set('response_code', $response->code);
 $this->set('response_body', $response->body);

 $this->render('/Cliente/respuesta');
 }

 public function rest_delete($id) {

 // remotely post the information to the server
 $link = $this->url . 'rest_productos/' . $id . '.json';
 $data = null;
 $httpSocket = new HttpSocket();
 $response = $httpSocket->delete($link, $data);
 $this->set('response_code', $response->code);
 $this->set('response_body', $response->body);

 $this->render('/Cliente/respuesta');
 }

}

Vista en View\Cliente\index.ctp:

 
<h1>Acciones</h1>
<p>
Escoja
<ul>
<li><?php echo $this->Html->link('Lista Productos', array('controller' => 'cliente', 'action' => 'rest_index')); ?></li><li><?php echo $this->Html->link('Ver producto con ID 1', array('controller' => 'cliente', 'action' => 'rest_view', 1)); ?></li>
<li><?php echo $this->Html->link('Añadir producto', array('controller' => 'cliente', 'action' => 'rest_add')); ?></li>
<li><?php echo $this->Html->link('Actualizar producto 2', array('controller' => 'cliente', 'action' => 'rest_edit', 2)); ?></li>
<li><?php echo $this->Html->link('Borrar producto 3', array('controller' => 'cliente', 'action' => 'rest_delete', 3)); ?></li></ul> </p>

Vista en View\Cliente\respuesta.ctp:

 
<h1>Código respuesta</h1>
<p><?php echo $response_code; ?></p>

<h1>Cuerpo respuesta</h1>
<p><?php echo $response_body; ?></p>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos