public function indexAction() { $client = new HttpClient(); $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); $method = $this->params()->fromQuery('method', 'get'); $client->setUri('http://api-rest/san-restful'); switch ($method) { case 'get': $id = $this->params()->fromQuery('id'); $client->setMethod('GET'); $client->setParameterGET(array('id' => $id)); break; case 'get-list': $client->setMethod('GET'); break; case 'create': $client->setMethod('POST'); $client->setParameterPOST(array('name' => 'samsonasik')); break; case 'update': $data = array('name' => 'ikhsan'); $adapter = $client->getAdapter(); $adapter->connect('localhost', 80); $uri = $client->getUri() . '?id=1'; // send with PUT Method, with $data parameter $adapter->write('PUT', new \Zend\Uri\Uri($uri), 1.1, array(), http_build_query($data)); $responsecurl = $adapter->read(); list($headers, $content) = explode("\r\n\r\n", $responsecurl, 2); $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('content-type', 'text/html; charset=utf-8'); $response->setContent($content); return $response; case 'delete': $adapter = $client->getAdapter(); $adapter->connect('localhost', 80); $uri = $client->getUri() . '?id=1'; //send parameter id = 1 // send with DELETE Method $adapter->write('DELETE', new \Zend\Uri\Uri($uri), 1.1, array()); $responsecurl = $adapter->read(); list($headers, $content) = explode("\r\n\r\n", $responsecurl, 2); $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('content-type', 'text/html; charset=utf-8'); $response->setContent($content); return $response; } //if get/get-list/create $response = $client->send(); if (!$response->isSuccess()) { // report failure $message = $response->getStatusCode() . ': ' . $response->getReasonPhrase(); $response = $this->getResponse(); $response->setContent($message); return $response; } $body = $response->getBody(); $response = $this->getResponse(); $response->setContent($body); return $response; }
public function indexAction() { $client = new HttpClient(); $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); $method = $this->params()->fromRoute('param', 'get-list'); $client->setUri('http://localhost/album-rest'); switch ($method) { case 'get': $id = $this->params()->fromRoute('id', 2); $client->setMethod('GET'); $client->setParameterGET(array('id' => $id)); break; case 'get-list': $client->setMethod('GET'); break; case 'create': $client->setMethod('POST'); $client->setParameterPOST(array('title' => 'Bob Marley LIVE', 'artist' => 'Bob Marley')); break; case 'update': $id = $this->params()->fromRoute('id', 2); $data = array('title' => 'Show 90 Anos Ao Vivo', 'artist' => 'Zeze di Camargo & Luciano'); $client->setMethod('PUT'); $client->setParameterPOST($data); $client->setParameterGET(array('id' => $id)); break; case 'delete': $id = $this->params()->fromRoute('id', 2); $client->setMethod('DELETE'); $client->setParameterGET(array('id' => $id)); break; } //send request $response = $client->send(); if (!$response->isSuccess()) { //error $message = $response->getStatusCode() . ': ' . $response->getReasonPhrase(); $response = $this->getResponse(); $response->setContent($message); return $response; } $body = $response->getBody(); $response = $this->getResponse(); $response->setContent($body); return $response; }
public function curl($url, array $params, $method = "POST") { $client = new HttpClient(); $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); $client->setUri($url); $client->setOptions(array('maxredirects' => 0, 'timeout' => 30)); $client->setMethod($method); $client->setHeaders(array('username: apiuser', 'password: 123456')); //if(!empty($params)) { if ($method == "POST" || $method == "PUT" || $method == "DELETE") { $client->setParameterPOST($params); } else { $client->setParameterGET($params); } //} $response = $client->send(); return $response; }
public function indexAction() { $client = new HttpClient(); $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); $method = $this->params()->fromQuery('method', 'get'); $client->setUri('http://posterlab.skilla.com/relatori/rest/ejemplo-servidor-restful'); switch ($method) { case 'get-list': $client->setMethod('GET'); break; case 'get': $client->setMethod('GET'); $client->setParameterGET(array('id' => 4)); break; case 'create': $client->setMethod('POST'); $client->setParameterPOST(array('data' => 'Programacion Java SE')); break; case 'update': $data = array('data' => 'Curso Spring Framework'); $adapter = $client->getAdapter(); $adapter->connect('posterlab.skilla.com', 80); $uri = $client->getUri() . '/2'; // Enviamos con Method PUT, con el parametro $data $adapter->write('PUT', new \Zend\Uri\Uri($uri), 1.1, array(), http_build_query($data)); $responsecurl = $adapter->read(); list($headers, $content) = explode("\r\n\r\n", $responsecurl, 2); $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('content-type', 'application/json; charset=utf-8'); $response->setContent($content); return $response; case 'delete': $adapter = $client->getAdapter(); $adapter->connect('posterlab.skilla.com', 80); $uri = $client->getUri() . '/1'; //enviamos param id = 1 // Enviamos con Method DELETE $adapter->write('DELETE', new \Zend\Uri\Uri($uri), 1.1, array()); $responsecurl = $adapter->read(); list($headers, $content) = explode("\r\n\r\n", $responsecurl, 2); $response = $this->getResponse(); $response->getHeaders()->addHeaderLine('content-type', 'application/json; charset=utf-8'); $response->setContent($content); return $response; default: $client->setMethod('GET'); break; } //enviamos get/get-list/create $response = $client->send(); if (!$response->isSuccess()) { // reportamos la falla $message = $response->getStatusCode() . ': ' . $response->getReasonPhrase(); $response = $this->getResponse(); $response->setContent($message); return $response; } $body = $response->getBody(); $response = $this->getResponse(); $response->setContent($body); return $response; }