示例#1
0
 public function run()
 {
     try {
         $response = $this->tryRun();
     } catch (\Exception $ex) {
         $response = new \Zend\Http\Response();
         $code = $ex->getCode();
         if (is_numeric($code) && $code >= 200 && $code <= 500) {
             $response->setStatusCode($code);
         } else {
             $response->setStatusCode(500);
         }
         $response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
         $response->setContent(\json_encode(array('exception' => get_class($ex), 'message' => $ex->getMessage())));
     }
     $response->getHeaders()->addHeaderLine('Access-Control-Allow-Origin', '*');
     /**
      * Imprime a resposta
      */
     $status = $response->renderStatusLine();
     header($status);
     foreach ($response->getHeaders() as $header) {
         if ($header instanceof MultipleHeaderInterface) {
             header($header->toString(), false);
             continue;
         }
         header($header->toString());
     }
     echo $response->getContent();
 }
 /**
  * Sends a request and returns a response
  *
  * @param CartRecover_Request $request
  * @return Cart_Recover_Response
  */
 public function sendRequest(CartRecover_Request $request)
 {
     $this->client->setUri($request->getUri());
     $this->client->setParameterGet($request->getParams());
     $this->client->setMethod($request->getMethod());
     $this->client->setHeaders(array('Accept' => 'application/json'));
     $this->response = $this->client->send();
     if ($this->response->getHeaders()->get('Content-Type')->getFieldValue() != 'application/json') {
         throw new CartRecover_Exception_UnexpectedValueException("Unknown response format.");
     }
     $body = json_decode($this->response->getContent(), true);
     $response = new CartRecover_Response();
     $response->setRawResponse($this->response->toString());
     $response->setBody($body);
     $response->setHeaders($this->response->getHeaders()->toArray());
     $response->setStatus($this->response->getReasonPhrase(), $this->response->getStatusCode());
     return $response;
 }