Created
June 7, 2017 20:07
-
-
Save casperbakker/8050a58753bb20264793693833b6a568 to your computer and use it in GitHub Desktop.
Easy PHP curl request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Picqer\Services\Curl; | |
| use Throwable; | |
| class CurlException extends \Exception | |
| { | |
| protected $response = null; | |
| protected $headers = []; | |
| public function __construct($message = "", $code = 0, $response, $headers = [], Throwable $previous = null) | |
| { | |
| parent::__construct($message, $code, $previous); | |
| $this->response = $response; | |
| $this->headers = $headers; | |
| } | |
| public function getResponse() | |
| { | |
| return $this->response; | |
| } | |
| public function getHeaders() | |
| { | |
| return $this->headers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Picqer\Services\Curl; | |
| class CurlRequest | |
| { | |
| const METHOD_GET = 'GET'; | |
| const METHOD_POST = 'POST'; | |
| const METHOD_PUT = 'PUT'; | |
| const METHOD_PATCH = 'PATCH'; | |
| const METHOD_DELETE = 'DELETE'; | |
| private $method; | |
| private $url; | |
| private $queryParams = []; | |
| private $headers = []; | |
| private $timeout = 30; | |
| private $postData = null; | |
| private $userAgent = 'Picqer (www.picqer.com)'; | |
| private $username = null; | |
| private $password = null; | |
| private $response; | |
| public function __construct($method = self::METHOD_GET, $url, $queryParams = []) | |
| { | |
| $this->method = $method; | |
| $this->url = $url; | |
| $this->queryParams = $queryParams; | |
| } | |
| public function execute() | |
| { | |
| $curlSession = curl_init(); | |
| $this->setBasicCurlSettings($curlSession); | |
| $this->setRequestSpecificCurlSettings($curlSession); | |
| $rawResponse = curl_exec($curlSession); | |
| $responseMetaData = collect(curl_getinfo($curlSession)); | |
| $curlErrorNo = curl_errno($curlSession); | |
| $curlErrorMessage = curl_error($curlSession); | |
| curl_close($curlSession); | |
| if (! $this->didRequestSucceed($responseMetaData, $curlErrorNo)) { | |
| $this->throwException($curlErrorNo, $curlErrorMessage, $responseMetaData, $rawResponse); | |
| } | |
| $this->response = new CurlResponse($rawResponse, $responseMetaData); | |
| return $this->response; | |
| } | |
| public function setPostBody($data) | |
| { | |
| if (is_array($data)) { | |
| $data = json_encode($data); | |
| $this->addJsonHeaders(); | |
| } | |
| $this->postData = $data; | |
| } | |
| public function addHeader($header) | |
| { | |
| $this->headers[] = $header; | |
| } | |
| public function setTimeout($seconds) | |
| { | |
| $this->timeout = $seconds; | |
| } | |
| public function addJsonHeaders() | |
| { | |
| $this->addHeader('Content-Type: application/json'); | |
| $this->addHeader('Accept: application/json'); | |
| } | |
| public function setUserAgent($userAgentString) | |
| { | |
| $this->userAgent = $userAgentString; | |
| } | |
| public function setUsername($username) | |
| { | |
| $this->username = $username; | |
| } | |
| public function setPassword($password) | |
| { | |
| $this->password = $password; | |
| } | |
| private function getHeaders() | |
| { | |
| return $this->headers; | |
| } | |
| private function throwException($curlErrorNo, $curlErrorMessage, $responseMetaData, $rawResponse) | |
| { | |
| switch ($responseMetaData->get('http_code')) { | |
| case '401': | |
| throw new UnauthorizedCurlException('Error', $responseMetaData->get('http_code'), $rawResponse); | |
| case '404': | |
| case '405': | |
| throw new NotFoundCurlException('Page not found', $responseMetaData->get('http_code'), $rawResponse); | |
| default: | |
| throw new UnknownCurlException($curlErrorMessage, $curlErrorNo, $rawResponse); | |
| } | |
| } | |
| private function didRequestSucceed($responseMetaData, $curlErrorNo) | |
| { | |
| if ($curlErrorNo) { | |
| return false; | |
| } | |
| return in_array($responseMetaData->get('http_code'), ['200', '201', '202', '204']); | |
| } | |
| private function getUserAgent() | |
| { | |
| return $this->userAgent; | |
| } | |
| private function getFullUrl() | |
| { | |
| $url = $this->url; | |
| if (! empty($this->queryParams)) { | |
| $url .= '?' . http_build_query($this->queryParams); | |
| } | |
| return $url; | |
| } | |
| private function getTimeout() | |
| { | |
| return $this->timeout; | |
| } | |
| private function getPostBody() | |
| { | |
| return $this->postData; | |
| } | |
| private function getMethod() | |
| { | |
| return strtoupper($this->method); | |
| } | |
| private function setBasicCurlSettings($curlSession) | |
| { | |
| curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($curlSession, CURLOPT_HEADER, true); | |
| curl_setopt($curlSession, CURLOPT_VERBOSE, true); | |
| curl_setopt($curlSession, CURLOPT_CONNECTTIMEOUT, 30); | |
| } | |
| private function setRequestSpecificCurlSettings($curlSession) | |
| { | |
| curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST, $this->getMethod()); | |
| curl_setopt($curlSession, CURLOPT_URL, $this->getFullUrl()); | |
| curl_setopt($curlSession, CURLOPT_TIMEOUT, $this->getTimeout()); | |
| curl_setopt($curlSession, CURLOPT_HTTPHEADER, $this->getHeaders()); | |
| curl_setopt($curlSession, CURLOPT_POSTFIELDS, $this->getPostBody()); | |
| if (! is_null($this->username) || ! is_null($this->password)) { | |
| curl_setopt($curlSession, CURLOPT_USERPWD, $this->username . ":" . $this->password); | |
| } | |
| curl_setopt($curlSession, CURLOPT_USERAGENT, $this->getUserAgent()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Picqer\Services\Curl; | |
| class CurlResponse | |
| { | |
| /** @var \Illuminate\Support\Collection */ | |
| private $headers; | |
| private $body = null; | |
| private $rawResponse; | |
| /** @var \Illuminate\Support\Collection */ | |
| private $metaData; | |
| public function __construct($rawResponse, $metaData = null) | |
| { | |
| $this->headers = collect(); | |
| $this->rawResponse = $rawResponse; | |
| $this->setMetaData($metaData); | |
| $this->parseRawResponse(); | |
| } | |
| public function getBody() | |
| { | |
| return $this->body; | |
| } | |
| public function getHeaders() | |
| { | |
| return $this->headers; | |
| } | |
| public function getContentType() | |
| { | |
| return $this->metaData->get('content_type'); | |
| } | |
| public function getHttpStatusCode() | |
| { | |
| return $this->metaData->get('http_code'); | |
| } | |
| private function parseRawResponse() | |
| { | |
| list($rawHeaders, $rawBody) = explode("\r\n\r\n", $this->rawResponse, 2); | |
| $this->parseRawHeaders($rawHeaders); | |
| $this->body = $rawBody; | |
| } | |
| private function setMetaData($metaData) | |
| { | |
| if (is_array($metaData)) { | |
| $metaData = collect($metaData); | |
| } | |
| if (is_null($metaData)) { | |
| $metaData = collect(); | |
| } | |
| $this->metaData = $metaData; | |
| } | |
| private function parseRawHeaders($headersAsString) | |
| { | |
| $headers = explode("\r\n", $headersAsString); | |
| foreach ($headers as $header) { | |
| $headerPieces = explode(':', $header, 2); | |
| if (! isset($headerPieces[0]) || ! isset($headerPieces[1])) { | |
| continue; | |
| } | |
| $this->headers->put($headerPieces[0], trim($headerPieces[1])); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $curl = new \Picqer\Services\Curl\CurlRequest('GET', 'https://requestb.in/xxxx'); | |
| $curlResponse = $curl->execute(); | |
| echo $curlResponse->getBody(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment