Ejemplo n.º 1
0
 /**
  * @param $uri
  * @param string $method
  * @param array $headers
  * @param null $body
  * @return B2Response
  */
 public function curl($uri, $method = 'GET', $headers = [], $body = null)
 {
     $response = new B2Response();
     $this->CurlRequest->setOption(CURLOPT_URL, $uri);
     $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method);
     $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1);
     $this->CurlRequest->setOption(CURLOPT_POST, 1);
     $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body);
     $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers);
     $this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, function ($curl, $header) use($response) {
         $response->addHeader($header);
         return strlen($header);
     });
     $resp = $this->CurlRequest->execute();
     if ($this->CurlRequest->getErrorNo() !== 0) {
         throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo());
     } else {
         $response->setData($resp);
         $response->setStatusCode($this->CurlRequest->getInfo(CURLINFO_HTTP_CODE));
         return $response;
     }
 }