| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <?php
- /**
- * Yii2 cURL wrapper
- * With RESTful support.
- *
- * @category Web-yii2
- * @package yii2-curl
- * @author Nils Gajsek <info@linslin.org>
- * @copyright 2013-2017 Nils Gajsek <info@linslin.org>
- * @license http://opensource.org/licenses/MIT MIT Public
- * @version 1.0.11
- * @link http://www.linslin.org
- *
- */
- namespace linslin\yii2\curl;
- use Yii;
- use yii\base\Exception;
- use yii\helpers\Json;
- use yii\web\HttpException;
- /**
- * cURL class
- */
- class Curl
- {
- // ################################################ class vars // ################################################
- /**
- * @var string
- * Holds response data right after sending a request.
- */
- public $response = null;
- /**
- * @var null|integer
- * Error code holder: https://curl.haxx.se/libcurl/c/libcurl-errors.html
- */
- public $errorCode = null;
- /**
- * @var integer HTTP-Status Code
- * This value will hold HTTP-Status Code. False if request was not successful.
- */
- public $responseCode = null;
- /**
- * @var string|null HTTP Response Charset
- * (taken from Content-type header)
- */
- public $responseCharset = null;
- /**
- * @var int HTTP Response Length
- * (taken from Content-length header, or strlen() of downloaded content)
- */
- public $responseLength = -1;
- /**
- * @var string|null HTTP Response Content Type
- * (taken from Content-type header)
- */
- public $responseType = null;
- /**
- * @var array|null HTTP Response headers
- * Lists response header in an array if CURLOPT_HEADER is set to true.
- */
- public $responseHeaders = null;
- /**
- * @var array HTTP-Status Code
- * Custom options holder
- */
- private $_options = [];
- /**
- * @var resource|null
- * Holds cURL-Handler
- */
- private $_curl = null;
- /**
- * @var array default curl options
- * Default curl options
- */
- private $_defaultOptions = [
- CURLOPT_USERAGENT => 'Yii2-Curl-Agent',
- CURLOPT_TIMEOUT => 30,
- CURLOPT_CONNECTTIMEOUT => 30,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HEADER => true,
- ];
- // ############################################### class methods // ##############################################
- /**
- * Start performing GET-HTTP-Request
- *
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded
- *
- * @return mixed response
- */
- public function get($url, $raw = true)
- {
- return $this->_httpRequest('GET', $url, $raw);
- }
- /**
- * Start performing HEAD-HTTP-Request
- *
- * @param string $url
- *
- * @return mixed response
- */
- public function head($url)
- {
- return $this->_httpRequest('HEAD', $url);
- }
- /**
- * Start performing POST-HTTP-Request
- *
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded
- *
- * @return mixed response
- */
- public function post($url, $raw = true)
- {
- return $this->_httpRequest('POST', $url, $raw);
- }
- /**
- * Start performing PUT-HTTP-Request
- *
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded
- *
- * @return mixed response
- */
- public function put($url, $raw = true)
- {
- return $this->_httpRequest('PUT', $url, $raw);
- }
- /**
- * Start performing PATCH-HTTP-Request
- *
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded
- *
- * @return mixed response
- */
- public function patch($url, $raw = true)
- {
- return $this->_httpRequest('PATCH', $url, $raw);
- }
- /**
- * Start performing DELETE-HTTP-Request
- *
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded
- *
- * @return mixed response
- */
- public function delete($url, $raw = true)
- {
- return $this->_httpRequest('DELETE', $url, $raw);
- }
- /**
- * Set curl option
- *
- * @param string $key
- * @param mixed $value
- *
- * @return $this
- */
- public function setOption($key, $value)
- {
- //set value
- if (array_key_exists($key, $this->_defaultOptions) && $key !== CURLOPT_WRITEFUNCTION) {
- $this->_defaultOptions[$key] = $value;
- } else {
- $this->_options[$key] = $value;
- }
- //return self
- return $this;
- }
- /**
- * Set curl options
- *
- * @param array $options
- *
- * @return $this
- */
- public function setOptions($options)
- {
- $this->_options = $options + $this->_options;
- return $this;
- }
- /**
- * Unset a single curl option
- *
- * @param string $key
- *
- * @return $this
- */
- public function unsetOption($key)
- {
- //reset a single option if its set already
- if (isset($this->_options[$key])) {
- unset($this->_options[$key]);
- }
- return $this;
- }
- /**
- * Unset all curl option, excluding default options.
- *
- * @return $this
- */
- public function unsetOptions()
- {
- //reset all options
- if (isset($this->_options)) {
- $this->_options = [];
- }
- return $this;
- }
- /**
- * Total reset of options, responses, etc.
- *
- * @return $this
- */
- public function reset()
- {
- if ($this->_curl !== null) {
- curl_close($this->_curl); //stop curl
- }
- //reset all options
- if (isset($this->_options)) {
- $this->_options = [];
- }
- //reset response & status params
- $this->_curl = null;
- $this->errorCode = null;
- $this->response = null;
- $this->responseCode = null;
- $this->responseCharset = null;
- $this->responseLength = -1;
- $this->responseType = null;
- return $this;
- }
- /**
- * Return a single option
- *
- * @param string|integer $key
- * @return mixed|boolean
- */
- public function getOption($key)
- {
- //get merged options depends on default and user options
- $mergesOptions = $this->getOptions();
- //return value or false if key is not set.
- return isset($mergesOptions[$key]) ? $mergesOptions[$key] : false;
- }
- /**
- * Return merged curl options and keep keys!
- *
- * @return array
- */
- public function getOptions()
- {
- return $this->_options + $this->_defaultOptions;
- }
- /**
- * Get curl info according to http://php.net/manual/de/function.curl-getinfo.php
- *
- * @param null $opt
- * @return array|mixed
- */
- 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 [];
- }
- }
- /**
- * Performs HTTP request
- *
- * @param string $method
- * @param string $url
- * @param boolean $raw if response body contains JSON and should be decoded -> helper.
- *
- * @throws Exception if request failed
- *
- * @return mixed
- */
- private function _httpRequest($method, $url, $raw = false)
- {
- //set request type and writer function
- $this->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($method));
- //check if method is head and set no body
- if ($method === 'HEAD') {
- $this->setOption(CURLOPT_NOBODY, true);
- $this->unsetOption(CURLOPT_WRITEFUNCTION);
- }
- //setup error reporting and profiling
- if (YII_DEBUG) {
- Yii::trace('Start sending cURL-Request: '.$url.'\n', __METHOD__);
- Yii::beginProfile($method.' '.$url.'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
- }
- /**
- * proceed curl
- */
- $curlOptions = $this->getOptions();
- $this->_curl = curl_init($url);
- curl_setopt_array($this->_curl, $curlOptions);
- $response = curl_exec($this->_curl);
- //check if curl was successful
- if ($response === false) {
- //set error code
- $this->errorCode = curl_errno($this->_curl);
- switch ($this->errorCode) {
- // 7, 28 = timeout
- case 7:
- case 28:
- $this->responseCode = 'timeout';
- return false;
- break;
- default:
- return false;
- break;
- }
- }
- //extract header / body data if CURLOPT_HEADER are set to true
- if (isset($curlOptions[CURLOPT_HEADER]) && $curlOptions[CURLOPT_HEADER]) {
- $this->response = $this->_extractCurlBody($response);
- $this->responseHeaders = $this->_extractCurlHeaders($response);
- } else {
- $this->response = $response;
- }
- // Extract additional curl params
- $this->_extractAdditionalCurlParameter();
- //end yii debug profile
- if (YII_DEBUG) {
- Yii::endProfile($method.' '.$url .'#'.md5(serialize($this->getOption(CURLOPT_POSTFIELDS))), __METHOD__);
- }
- //check responseCode and return data/status
- if ($this->getOption(CURLOPT_CUSTOMREQUEST) === 'HEAD') {
- return true;
- } else {
- $this->response = $raw ? $this->response : Json::decode($this->response);
- return $this->response;
- }
- }
- /**
- * Extract additional curl params private class helper
- */
- private function _extractAdditionalCurlParameter ()
- {
- /**
- * retrieve response code
- */
- $this->responseCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
- /**
- * try extract response type & charset.
- */
- $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);
- //extract charset
- if (preg_match('~^charset=(.+?)$~', trim($possibleCharset), $matches) && isset($matches[1])) {
- $this->responseCharset = strtolower($matches[1]);
- }
- }
- /**
- * try extract response length
- */
- $this->responseLength = curl_getinfo($this->_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
- if((int)$this->responseLength == -1) {
- $this->responseLength = strlen($this->response);
- }
- }
- /**
- * Extract body curl data from response
- *
- * @param string $response
- * @return string
- */
- private function _extractCurlBody ($response)
- {
- return substr($response, $this->getInfo(CURLINFO_HEADER_SIZE));
- }
- /**
- * Extract header curl data from response
- *
- * @param string $response
- * @return array
- */
- private function _extractCurlHeaders ($response)
- {
- //Init
- $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);
- $headers[$key] = $value;
- }
- }
- return $headers;
- }
- }
|