123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <?php
- namespace components;
- use Yii;
- use yii\base\Exception;
- use yii\helpers\Json;
- class Curl
- {
-
- public $httpType = 'http';
-
- public $response = null;
-
- public $errorCode = null;
-
- public $errorText = null;
-
- public $responseCode = null;
-
- public $responseCharset = null;
-
- public $responseLength = -1;
-
- public $responseType = null;
-
- public $responseHeaders = null;
-
- protected $_options = [];
-
- protected $_getParams = [];
-
- protected $_postParams = [];
-
- public $curl = null;
-
- protected $_baseUrl = '';
-
- protected $_defaultOptions = [
- CURLOPT_USERAGENT => 'Yii2-Curl-Agent',
- CURLOPT_TIMEOUT => 10,
- CURLOPT_CONNECTTIMEOUT => 10,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HEADER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => 2,
- ];
-
-
- public function get($url, $raw = true)
- {
- $this->_baseUrl = $url;
- return $this->_httpRequest('GET', $raw);
- }
-
- public function head($url)
- {
- $this->_baseUrl = $url;
- return $this->_httpRequest('HEAD');
- }
-
- public function post($url, $raw = true)
- {
- $this->_baseUrl = $url;
- return $this->_httpRequest('POST', $raw);
- }
-
- public function put($url, $raw = true)
- {
- $this->_baseUrl = $url;
- return $this->_httpRequest('PUT', $raw);
- }
-
- public function patch($url, $raw = true)
- {
- $this->_baseUrl = $url;
- $this->setHeaders([
- 'X-HTTP-Method-Override' => 'PATCH'
- ]);
- return $this->_httpRequest('POST',$raw);
- }
-
- public function delete($url, $raw = true)
- {
- $this->_baseUrl = $url;
- return $this->_httpRequest('DELETE', $raw);
- }
-
- public function setOption($key, $value)
- {
-
- if (array_key_exists($key, $this->_defaultOptions) && $key !== CURLOPT_WRITEFUNCTION) {
- $this->_defaultOptions[$key] = $value;
- } else {
- $this->_options[$key] = $value;
- }
-
- return $this;
- }
-
- public function setGetParams($params)
- {
- if (is_array($params)) {
- foreach ($params as $key => $value) {
- $this->_getParams[$key] = $value;
- }
- }
-
- return $this;
- }
-
- public function setPostParams($params)
- {
- if (is_array($params)) {
- $this->setOption(
- CURLOPT_POSTFIELDS,
- http_build_query($params)
- );
- }
-
- return $this;
- }
-
- public function setRawPostData($data)
- {
- $this->setOption(
- CURLOPT_POSTFIELDS,
- $data
- );
-
- return $this;
- }
-
- public function setRequestBody($data)
- {
- if (is_string($data)) {
- $this->setOption(
- CURLOPT_POSTFIELDS,
- $data
- );
- }
-
- return $this;
- }
-
- public function getUrl()
- {
- if (Count($this->_getParams) > 0) {
- return $this->_baseUrl.'?'.http_build_query($this->_getParams);
- } else {
- return $this->_baseUrl;
- }
- }
-
- public function setOptions($options)
- {
- $this->_options = $options + $this->_options;
- return $this;
- }
-
- public function setHeaders($headers)
- {
- if (is_array($headers)) {
-
- $parsedHeader = [];
-
- foreach ($this->getRequestHeaders() as $header => $value) {
- array_push($parsedHeader, $header.':'.$value);
- }
-
- foreach ($headers as $header => $value) {
- array_push($parsedHeader, $header.':'.$value);
- }
-
- $this->setOption(
- CURLOPT_HTTPHEADER,
- $parsedHeader
- );
- }
- return $this;
- }
-
- public function setHeader($header, $value)
- {
-
- $parsedHeader = [];
-
- foreach ($this->getRequestHeaders() as $headerToSet => $valueToSet) {
- array_push($parsedHeader, $headerToSet.':'.$valueToSet);
- }
-
- if (strlen($header) > 0) {
- array_push($parsedHeader, $header.':'.$value);
- }
-
- $this->setOption(
- CURLOPT_HTTPHEADER,
- $parsedHeader
- );
- return $this;
- }
-
- public function unsetHeader($header)
- {
-
- $parsedHeader = [];
-
- foreach ($this->getRequestHeaders() as $headerToSet => $valueToSet) {
- if ($header !== $headerToSet) {
- array_push($parsedHeader, $headerToSet.':'.$valueToSet);
- }
- }
-
- $this->setOption(
- CURLOPT_HTTPHEADER,
- $parsedHeader
- );
- return $this;
- }
-
- public function getRequestHeaders()
- {
-
- $requestHeaders = $this->getOption(CURLOPT_HTTPHEADER);
- $parsedRequestHeaders = [];
- if (is_array($requestHeaders)) {
- foreach ($requestHeaders as $headerValue) {
- list ($key, $value) = explode(':', $headerValue, 2);
- $parsedRequestHeaders[$key] = $value;
- }
- }
- return $parsedRequestHeaders;
- }
-
- public function getRequestHeader($headerKey)
- {
-
- $parsedRequestHeaders = $this->getRequestHeaders();
- return isset($parsedRequestHeaders[$headerKey]) ? $parsedRequestHeaders[$headerKey] : null;
- }
-
- public function unsetOption($key)
- {
-
- if (isset($this->_options[$key])) {
- unset($this->_options[$key]);
- }
- return $this;
- }
-
- public function unsetOptions()
- {
-
- if (isset($this->_options)) {
- $this->_options = [];
- }
- return $this;
- }
-
- public function reset()
- {
- if ($this->curl !== null) {
- curl_close($this->curl);
- }
-
- if (isset($this->_options)) {
- $this->_options = [];
- }
-
- $this->curl = null;
- $this->errorCode = null;
- $this->response = null;
- $this->responseCode = null;
- $this->responseCharset = null;
- $this->responseLength = -1;
- $this->responseType = null;
- $this->errorText = null;
- $this->_postParams = [];
- $this->_getParams = [];
- return $this;
- }
-
- public function getOption($key)
- {
-
- $mergesOptions = $this->getOptions();
-
- return isset($mergesOptions[$key]) ? $mergesOptions[$key] : false;
- }
-
- public function getOptions()
- {
- return $this->_options + $this->_defaultOptions;
- }
-
- public function getInfo($opt = null)
- {
- if ($this->curl !== null && $opt === null) {
- return curl_getinfo($this->curl);
- } elseif ($this->curl !== null && $opt !== null) {
- return curl_getinfo($this->curl, $opt);
- } else {
- return [];
- }
- }
-
- protected function _httpRequest($method, $raw = false)
- {
-
- $this->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($method));
-
- if ($method === 'HEAD') {
- $this->setOption(CURLOPT_NOBODY, true);
- $this->unsetOption(CURLOPT_WRITEFUNCTION);
- }
-
- if (YII_DEBUG) {
- Yii::trace('Start sending cURL-Request: '.$this->getUrl().'\n', __METHOD__);
- Yii::beginProfile($method.' '.$this->_baseUrl.'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
- }
-
- $curlOptions = $this->getOptions();
- $this->curl = curl_init($this->getUrl());
- curl_setopt_array($this->curl, $curlOptions);
- $response = curl_exec($this->curl);
-
- if ($response === false) {
-
- $this->errorCode = curl_errno($this->curl);
- $this->errorText = curl_strerror($this->errorCode);
- switch ($this->errorCode) {
-
- case 7:
- case 28:
- $this->responseCode = 'timeout';
- return false;
- break;
- default:
- return false;
- break;
- }
- }
-
- if (isset($curlOptions[CURLOPT_HEADER]) && $curlOptions[CURLOPT_HEADER]) {
- $this->response = $this->_extractCurlBody($response);
- $this->responseHeaders = $this->_extractCurlHeaders($response);
- } else {
- $this->response = $response;
- }
-
- $this->_extractAdditionalCurlParameter();
-
- if (YII_DEBUG) {
- Yii::endProfile($method.' '.$this->getUrl().'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
- }
-
- if ($this->getOption(CURLOPT_CUSTOMREQUEST) === 'HEAD') {
- return true;
- } else {
- $this->response = $raw ? $this->response : Json::decode($this->response);
- return $this->response;
- }
- }
-
- protected function _extractAdditionalCurlParameter ()
- {
-
- $this->responseCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
-
- $this->responseType = curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE);
- if (!is_null($this->responseType) && count(explode(';', $this->responseType)) > 1) {
- list($this->responseType, $possibleCharset) = explode(';', $this->responseType);
-
- if (preg_match('~^charset=(.+?)$~', trim($possibleCharset), $matches) && isset($matches[1])) {
- $this->responseCharset = strtolower($matches[1]);
- }
- }
-
- $this->responseLength = curl_getinfo($this->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
- if((int)$this->responseLength == -1) {
- $this->responseLength = strlen($this->response);
- }
- }
-
- protected function _extractCurlBody ($response)
- {
- return substr($response, $this->getInfo(CURLINFO_HEADER_SIZE));
- }
-
- protected function _extractCurlHeaders ($response)
- {
-
- $headers = [];
- $headerText = substr($response, 0, strpos($response, "\r\n\r\n"));
- foreach (explode("\r\n", $headerText) as $i => $line) {
- if ($i === 0) {
- $headers['http_code'] = $line;
- } else {
- list ($key, $value) = explode(':', $line, 2);
- $headers[$key] = ltrim($value);
- }
- }
- return $headers;
- }
- }
|