实现请求缓存1天。

This commit is contained in:
2025-04-18 15:38:58 +08:00
parent 2d3f7d8511
commit 96da83a1ab
209 changed files with 19400 additions and 1657 deletions

View File

@@ -1,8 +0,0 @@
root = true
[*.php]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
trim_trailing_whitespace = true

View File

@@ -6,6 +6,82 @@ backwards-incompatible changes that will affect existing usage.
<!-- CHANGELOG_PLACEHOLDER -->
## 9.19.2 - 2024-04-09
- Fix CI: Use nullable type declaration ([#859](https://github.com/php-curl-class/php-curl-class/pull/859))
## 9.19.1 - 2024-02-27
- Fix afterSend not being called ([#848](https://github.com/php-curl-class/php-curl-class/pull/848))
## 9.19.0 - 2024-01-18
- Allow displaying curl option value without specifying value ([#837](https://github.com/php-curl-class/php-curl-class/pull/837))
## 9.18.2 - 2023-09-11
- Fix use of mb_strpos() causing error when polyfill is used ([#813](https://github.com/php-curl-class/php-curl-class/pull/813))
## 9.18.1 - 2023-08-29
- Add additional check for decoding gzip-encoded responses ([#808](https://github.com/php-curl-class/php-curl-class/pull/808))
## 9.18.0 - 2023-08-28
- Implement Curl::setError() and MultiCurl::setError() ([#805](https://github.com/php-curl-class/php-curl-class/pull/805))
- Rename ::setError() to ::afterSend() ([#807](https://github.com/php-curl-class/php-curl-class/pull/807))
## 9.17.4 - 2023-07-10
- Add coding standards rule to use the null coalescing operator ?? where possible ([#801](https://github.com/php-curl-class/php-curl-class/pull/801))
- Replace isset with null coalescing operator ([#800](https://github.com/php-curl-class/php-curl-class/pull/800))
## 9.17.3 - 2023-07-04
- Update PHP_CodeSniffer ruleset: PSR2 → PSR12 ([#797](https://github.com/php-curl-class/php-curl-class/pull/797))
- Add additional coding standard checks ([#796](https://github.com/php-curl-class/php-curl-class/pull/796))
## 9.17.2 - 2023-06-27
- Use short array syntax ([#793](https://github.com/php-curl-class/php-curl-class/pull/793))
- Add PHP-CS-Fixer to check for unused imports ([#794](https://github.com/php-curl-class/php-curl-class/pull/794))
- Replace `uniqid` by `random_bytes` ([#792](https://github.com/php-curl-class/php-curl-class/pull/792))
## 9.17.1 - 2023-06-14
- Improve and add tests for Curl::fastDownload() ([#791](https://github.com/php-curl-class/php-curl-class/pull/791))
## 9.17.0 - 2023-06-13
- Make method to display curl option value public ([#790](https://github.com/php-curl-class/php-curl-class/pull/790))
## 9.16.1 - 2023-06-12
- Differentiate between internal options and user-set options ([#788](https://github.com/php-curl-class/php-curl-class/pull/788))
- Create method to display a curl option value ([#785](https://github.com/php-curl-class/php-curl-class/pull/785))
- Fix existing header overwritten after using MultiCurl::addCurl() ([#787](https://github.com/php-curl-class/php-curl-class/pull/787))
## 9.16.0 - 2023-05-25
- Graduate Curl::fastDownload() ([#783](https://github.com/php-curl-class/php-curl-class/pull/783))
## 9.15.1 - 2023-05-24
- Fix PHP CodeSniffer errors ([#782](https://github.com/php-curl-class/php-curl-class/pull/782))
## 9.15.0 - 2023-05-22
- Update Curl::diagnose() to detect bit flags with negative values ([#781](https://github.com/php-curl-class/php-curl-class/pull/781))
- Display bit flags in use when calling Curl::diagnose() ([#779](https://github.com/php-curl-class/php-curl-class/pull/779))
## 9.14.5 - 2023-05-16
- Handle missing content-type response header in Curl::diagnose() ([#778](https://github.com/php-curl-class/php-curl-class/pull/778))
## 9.14.4 - 2023-05-08
- Update article in Curl::diagnose() Allow header warning ([#776](https://github.com/php-curl-class/php-curl-class/pull/776))
## 9.14.3 - 2023-03-13
- Remove use of array_merge() inside loop ([#774](https://github.com/php-curl-class/php-curl-class/pull/774))

View File

@@ -37,7 +37,7 @@ Installation instructions to use the `composer` command can be found on https://
### Requirements
PHP Curl Class works with PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, and 8.2.
PHP Curl Class works with PHP 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, and 7.0.
### Quick Start and Examples
@@ -199,7 +199,7 @@ More examples are available under [/examples](https://github.com/php-curl-class/
Curl::__construct($base_url = null, $options = [])
Curl::__destruct()
Curl::__get($name)
Curl::_fastDownload($url, $filename, $connections = 4)
Curl::afterSend($callback)
Curl::attemptRetry()
Curl::beforeSend($callback)
Curl::buildPostData($data)
@@ -209,10 +209,12 @@ Curl::complete($callback)
Curl::delete($url, $query_parameters = [], $data = [])
Curl::diagnose($return = false)
Curl::disableTimeout()
Curl::displayCurlOptionValue($option, $value = null)
Curl::download($url, $mixed_filename)
Curl::error($callback)
Curl::exec($ch = null)
Curl::execDone()
Curl::fastDownload($url, $filename, $connections = 4)
Curl::get($url, $data = [])
Curl::getAttempts()
Curl::getBeforeSendCallback()
@@ -233,6 +235,7 @@ Curl::getId()
Curl::getInfo($opt = null)
Curl::getJsonDecoder()
Curl::getOpt($option)
Curl::getOptions()
Curl::getRawResponse()
Curl::getRawResponseHeaders()
Curl::getRemainingRetries()
@@ -245,6 +248,7 @@ Curl::getRetries()
Curl::getRetryDecider()
Curl::getSuccessCallback()
Curl::getUrl()
Curl::getUserSetOptions()
Curl::getXmlDecoder()
Curl::head($url, $data = [])
Curl::isChildOfMultiCurl()
@@ -319,6 +323,7 @@ MultiCurl::addPatch($url, $data = [])
MultiCurl::addPost($url, $data = '', $follow_303_with_post = false)
MultiCurl::addPut($url, $data = [])
MultiCurl::addSearch($url, $data = [])
MultiCurl::afterSend($callback)
MultiCurl::beforeSend($callback)
MultiCurl::close()
MultiCurl::complete($callback)

View File

@@ -11,6 +11,10 @@
"authors": [
{
"name": "Zach Borboa"
},
{
"name": "Contributors",
"homepage": "https://github.com/php-curl-class/php-curl-class/graphs/contributors"
}
],
"require": {
@@ -20,11 +24,12 @@
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "*",
"ext-gd": "*",
"friendsofphp/php-cs-fixer": "*",
"phpcompatibility/php-compatibility": "dev-develop",
"phpcsstandards/phpcsutils": "@alpha",
"phpunit/phpunit": "*",
"squizlabs/php_codesniffer": "*",
"vimeo/psalm": "*"
"vimeo/psalm": ">=0.3.63"
},
"suggest": {
"ext-mbstring": "*"

View File

@@ -0,0 +1,2 @@
[flake8]
max-line-length = 100

View File

@@ -1,18 +1,16 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
use Curl\CaseInsensitiveArray;
class ArrayUtil
{
/**
* Is Array Assoc
*
* @access public
* @param $array
*
* @return boolean
* @param $array
* @return bool
*/
public static function isArrayAssoc($array)
{
@@ -26,10 +24,8 @@ class ArrayUtil
* Is Array Assoc
*
* @deprecated Use ArrayUtil::isArrayAssoc().
* @access public
* @param $array
*
* @return boolean
* @param $array
* @return bool
*/
public static function is_array_assoc($array)
{
@@ -39,10 +35,8 @@ class ArrayUtil
/**
* Is Array Multidim
*
* @access public
* @param $array
*
* @return boolean
* @param $array
* @return bool
*/
public static function isArrayMultidim($array)
{
@@ -57,10 +51,8 @@ class ArrayUtil
* Is Array Multidim
*
* @deprecated Use ArrayUtil::isArrayMultidim().
* @access public
* @param $array
*
* @return boolean
* @param $array
* @return bool
*/
public static function is_array_multidim($array)
{
@@ -70,10 +62,8 @@ class ArrayUtil
/**
* Array Flatten Multidim
*
* @access public
* @param $array
* @param $prefix
*
* @param $array
* @param $prefix
* @return array
*/
public static function arrayFlattenMultidim($array, $prefix = false)
@@ -124,10 +114,8 @@ class ArrayUtil
* Array Flatten Multidim
*
* @deprecated Use ArrayUtil::arrayFlattenMultidim().
* @access public
* @param $array
* @param $prefix
*
* @param $array
* @param $prefix
* @return array
*/
public static function array_flatten_multidim($array, $prefix = false)
@@ -138,9 +126,7 @@ class ArrayUtil
/**
* Array Random
*
* @access public
* @param $array
*
* @param $array
* @return mixed
*/
public static function arrayRandom($array)
@@ -151,10 +137,8 @@ class ArrayUtil
/**
* Array Random Index
*
* @access public
* @param $array
*
* @return integer
* @param $array
* @return int
*/
public static function arrayRandomIndex($array)
{
@@ -165,9 +149,7 @@ class ArrayUtil
* Array Random
*
* @deprecated Use ArrayUtil::arrayRandom().
* @access public
* @param $array
*
* @param $array
* @return mixed
*/
public static function array_random($array)

View File

@@ -1,21 +1,24 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
abstract class BaseCurl
{
public $beforeSendCallback = null;
public $afterSendCallback = null;
public $successCallback = null;
public $errorCallback = null;
public $completeCallback = null;
protected $options = [];
protected $userSetOptions = [];
/**
* Before Send
*
* @access public
* @param $callback callable|null
* @param $callback callable|null
*/
public function beforeSend($callback)
{
@@ -27,8 +30,7 @@ abstract class BaseCurl
/**
* Complete
*
* @access public
* @param $callback callable|null
* @param $callback callable|null
*/
public function complete($callback)
{
@@ -37,8 +39,6 @@ abstract class BaseCurl
/**
* Disable Timeout
*
* @access public
*/
public function disableTimeout()
{
@@ -48,8 +48,7 @@ abstract class BaseCurl
/**
* Error
*
* @access public
* @param $callback callable|null
* @param $callback callable|null
*/
public function error($callback)
{
@@ -59,14 +58,12 @@ abstract class BaseCurl
/**
* Get Opt
*
* @access public
* @param $option
*
* @param $option
* @return mixed
*/
public function getOpt($option)
{
return isset($this->options[$option]) ? $this->options[$option] : null;
return $this->options[$option] ?? null;
}
/**
@@ -75,8 +72,7 @@ abstract class BaseCurl
* Remove an internal header from the request.
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
*
* @access public
* @param $key
* @param $key
*/
public function removeHeader($key)
{
@@ -86,7 +82,7 @@ abstract class BaseCurl
/**
* Set auto referer
*
* @access public
* @param mixed $auto_referer
*/
public function setAutoReferer($auto_referer = true)
{
@@ -96,7 +92,7 @@ abstract class BaseCurl
/**
* Set auto referrer
*
* @access public
* @param mixed $auto_referrer
*/
public function setAutoReferrer($auto_referrer = true)
{
@@ -106,9 +102,8 @@ abstract class BaseCurl
/**
* Set Basic Authentication
*
* @access public
* @param $username
* @param $password
* @param $username
* @param $password
*/
public function setBasicAuthentication($username, $password = '')
{
@@ -119,8 +114,7 @@ abstract class BaseCurl
/**
* Set Connect Timeout
*
* @access public
* @param $seconds
* @param $seconds
*/
public function setConnectTimeout($seconds)
{
@@ -136,9 +130,8 @@ abstract class BaseCurl
/**
* Set Digest Authentication
*
* @access public
* @param $username
* @param $password
* @param $username
* @param $password
*/
public function setDigestAuthentication($username, $password = '')
{
@@ -146,21 +139,48 @@ abstract class BaseCurl
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
}
/**
* After Send
*
* This function is called after the request has been sent.
*
* It can be used to override whether or not the request errored. The
* instance is passed as the first argument to the function and the instance
* has attributes like $instance->httpStatusCode and $instance->response to
* help decide if the request errored. Set $instance->error to true or false
* within the function.
*
* When $instance->error is true indicating a request error, the error
* callback set by Curl::error() is called. When $instance->error is false,
* the success callback set by Curl::success() is called.
*
* @param $callback callable|null
*/
public function afterSend($callback)
{
$this->afterSendCallback = $callback;
}
/**
* Set File
*
* @access public
* @param $file
* @param $file
*/
public function setFile($file)
{
$this->setOpt(CURLOPT_FILE, $file);
}
protected function setFileInternal($file)
{
$this->setOptInternal(CURLOPT_FILE, $file);
}
/**
* Set follow location
*
* @access public
* @param mixed $follow_location
* @see Curl::setMaximumRedirects()
*/
public function setFollowLocation($follow_location = true)
{
@@ -170,7 +190,7 @@ abstract class BaseCurl
/**
* Set forbid reuse
*
* @access public
* @param mixed $forbid_reuse
*/
public function setForbidReuse($forbid_reuse = true)
{
@@ -186,8 +206,7 @@ abstract class BaseCurl
* The name of the outgoing network interface to use.
* This can be an interface name, an IP address or a host name.
*
* @access public
* @param $interface
* @param $interface
*/
public function setInterface($interface)
{
@@ -199,7 +218,8 @@ abstract class BaseCurl
/**
* Set maximum redirects
*
* @access public
* @param mixed $maximum_redirects
* @see Curl::setFollowLocation()
*/
public function setMaximumRedirects($maximum_redirects)
{
@@ -207,13 +227,17 @@ abstract class BaseCurl
}
abstract public function setOpt($option, $value);
protected function setOptInternal($option, $value)
{
}
abstract public function setOpts($options);
/**
* Set Port
*
* @access public
* @param $port
* @param $port
*/
public function setPort($port)
{
@@ -225,11 +249,10 @@ abstract class BaseCurl
*
* Set an HTTP proxy to tunnel requests through.
*
* @access public
* @param $proxy - The HTTP proxy to tunnel requests through. May include port number.
* @param $port - The port number of the proxy to connect to. This port number can also be set in $proxy.
* @param $username - The username to use for the connection to the proxy.
* @param $password - The password to use for the connection to the proxy.
* @param $proxy - The HTTP proxy to tunnel requests through. May include port number.
* @param $port - The port number of the proxy to connect to. This port number can also be set in $proxy.
* @param $username - The username to use for the connection to the proxy.
* @param $password - The password to use for the connection to the proxy.
*/
public function setProxy($proxy, $port = null, $username = null, $password = null)
{
@@ -247,8 +270,7 @@ abstract class BaseCurl
*
* Set the HTTP authentication method(s) to use for the proxy connection.
*
* @access public
* @param $auth
* @param $auth
*/
public function setProxyAuth($auth)
{
@@ -260,8 +282,7 @@ abstract class BaseCurl
*
* Set the proxy to tunnel through HTTP proxy.
*
* @access public
* @param $tunnel boolean
* @param $tunnel boolean
*/
public function setProxyTunnel($tunnel = true)
{
@@ -273,8 +294,7 @@ abstract class BaseCurl
*
* Set the proxy protocol type.
*
* @access public
* @param $type
* @param $type
*/
public function setProxyType($type)
{
@@ -284,19 +304,22 @@ abstract class BaseCurl
/**
* Set Range
*
* @access public
* @param $range
* @param $range
*/
public function setRange($range)
{
$this->setOpt(CURLOPT_RANGE, $range);
}
protected function setRangeInternal($range)
{
$this->setOptInternal(CURLOPT_RANGE, $range);
}
/**
* Set Referer
*
* @access public
* @param $referer
* @param $referer
*/
public function setReferer($referer)
{
@@ -306,8 +329,7 @@ abstract class BaseCurl
/**
* Set Referrer
*
* @access public
* @param $referrer
* @param $referrer
*/
public function setReferrer($referrer)
{
@@ -319,35 +341,42 @@ abstract class BaseCurl
/**
* Set Timeout
*
* @access public
* @param $seconds
* @param $seconds
*/
public function setTimeout($seconds)
{
$this->setOpt(CURLOPT_TIMEOUT, $seconds);
}
protected function setTimeoutInternal($seconds)
{
$this->setOptInternal(CURLOPT_TIMEOUT, $seconds);
}
abstract public function setUrl($url, $mixed_data = '');
/**
* Set User Agent
*
* @access public
* @param $user_agent
* @param $user_agent
*/
public function setUserAgent($user_agent)
{
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
}
protected function setUserAgentInternal($user_agent)
{
$this->setOptInternal(CURLOPT_USERAGENT, $user_agent);
}
abstract public function setXmlDecoder($mixed);
abstract public function stop();
/**
* Success
*
* @access public
* @param $callback callable|null
* @param $callback callable|null
*/
public function success($callback)
{
@@ -360,8 +389,6 @@ abstract class BaseCurl
* Unset Proxy
*
* Disable use of the proxy.
*
* @access public
*/
public function unsetProxy()
{
@@ -371,9 +398,8 @@ abstract class BaseCurl
/**
* Verbose
*
* @access public
* @param bool $on
* @param resource|string $output
* @param bool $on
* @param resource|string $output
*/
public function verbose($on = true, $output = 'STDERR')
{

View File

@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
@@ -7,9 +9,9 @@ namespace Curl;
*/
class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
{
/**
* @var mixed[] Data storage with lowercase keys.
*
* @see offsetSet()
* @see offsetExists()
* @see offsetUnset()
@@ -23,6 +25,7 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
/**
* @var string[] Case-sensitive keys.
*
* @see offsetSet()
* @see offsetUnset()
* @see key()
@@ -36,13 +39,15 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
* case-insensitive array. Caution: Data may be lost when converting
* case-sensitive arrays to case-insensitive arrays.
*
* @param mixed[] $initial (optional) Existing array to convert.
*
* @param mixed[] $initial (optional) Existing array to convert.
* @return CaseInsensitiveArray
*
* @access public
*/
public function __construct(array $initial = null)
// TODO: Use a nullable type declaration when supported versions >= PHP 7.1.
// Trying to use the nullable type declaration on PHP 7.0:
// public function __construct(?array $initial = null)
// results in:
// ParseError: syntax error, unexpected '?', expecting variable (T_VARIABLE)
public function __construct($initial = null)
{
if ($initial !== null) {
foreach ($initial as $key => $value) {
@@ -58,14 +63,10 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
* stores the case-sensitive offset and the data at the lowercase indexes in
* $this->keys and @this->data.
*
* @see https://secure.php.net/manual/en/arrayaccess.offsetset.php
*
* @param string $offset The offset to store the data at (case-insensitive).
* @param mixed $value The data to store at the specified offset.
*
* @param string $offset The offset to store the data at (case-insensitive).
* @param mixed $value The data to store at the specified offset.
* @return void
*
* @access public
* @see https://secure.php.net/manual/en/arrayaccess.offsetset.php
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
@@ -85,13 +86,9 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
* Checks if the offset exists in data storage. The index is looked up with
* the lowercase version of the provided offset.
*
* @param string $offset Offset to check
* @return bool If the offset exists.
* @see https://secure.php.net/manual/en/arrayaccess.offsetexists.php
*
* @param string $offset Offset to check
*
* @return bool If the offset exists.
*
* @access public
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
@@ -105,13 +102,9 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
* Unsets the specified offset. Converts the provided offset to lowercase,
* and unsets the case-sensitive key, as well as the stored data.
*
* @see https://secure.php.net/manual/en/arrayaccess.offsetunset.php
*
* @param string $offset The offset to unset.
*
* @param string $offset The offset to unset.
* @return void
*
* @access public
* @see https://secure.php.net/manual/en/arrayaccess.offsetunset.php
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
@@ -127,31 +120,23 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
* Return the stored data at the provided offset. The offset is converted to
* lowercase and the lookup is done on the data store directly.
*
* @param string $offset Offset to lookup.
* @return mixed The data stored at the offset.
* @see https://secure.php.net/manual/en/arrayaccess.offsetget.php
*
* @param string $offset Offset to lookup.
*
* @return mixed The data stored at the offset.
*
* @access public
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$offsetlower = strtolower($offset);
return isset($this->data[$offsetlower]) ? $this->data[$offsetlower] : null;
return $this->data[$offsetlower] ?? null;
}
/**
* Count
*
* @see https://secure.php.net/manual/en/countable.count.php
*
* @param void
*
* @return integer The number of elements stored in the array.
*
* @access public
* @return int The number of elements stored in the array.
* @see https://secure.php.net/manual/en/countable.count.php
*/
#[\ReturnTypeWillChange]
public function count()
@@ -162,13 +147,9 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
/**
* Current
*
* @see https://secure.php.net/manual/en/iterator.current.php
*
* @param void
*
* @return mixed Data at the current position.
*
* @access public
* @see https://secure.php.net/manual/en/iterator.current.php
*/
#[\ReturnTypeWillChange]
public function current()
@@ -179,13 +160,9 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
/**
* Next
*
* @see https://secure.php.net/manual/en/iterator.next.php
*
* @param void
*
* @return void
*
* @access public
* @see https://secure.php.net/manual/en/iterator.next.php
*/
#[\ReturnTypeWillChange]
public function next()
@@ -196,29 +173,22 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
/**
* Key
*
* @see https://secure.php.net/manual/en/iterator.key.php
*
* @param void
*
* @return mixed Case-sensitive key at current position.
*
* @access public
* @see https://secure.php.net/manual/en/iterator.key.php
*/
#[\ReturnTypeWillChange]
public function key()
{
$key = key($this->data);
return isset($this->keys[$key]) ? $this->keys[$key] : $key;
return $this->keys[$key] ?? $key;
}
/**
* Valid
*
* @see https://secure.php.net/manual/en/iterator.valid.php
*
* @return bool If the current position is valid.
*
* @access public
* @see https://secure.php.net/manual/en/iterator.valid.php
*/
#[\ReturnTypeWillChange]
public function valid()
@@ -229,13 +199,9 @@ class CaseInsensitiveArray implements \ArrayAccess, \Countable, \Iterator
/**
* Rewind
*
* @see https://secure.php.net/manual/en/iterator.rewind.php
*
* @param void
*
* @return void
*
* @access public
* @see https://secure.php.net/manual/en/iterator.rewind.php
*/
#[\ReturnTypeWillChange]
public function rewind()

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
@@ -7,11 +9,10 @@ class Decoder
/**
* Decode JSON
*
* @access public
* @param $json
* @param $assoc
* @param $depth
* @param $options
* @param $json
* @param $assoc
* @param $depth
* @param $options
*/
public static function decodeJson()
{
@@ -26,12 +27,11 @@ class Decoder
/**
* Decode XML
*
* @access public
* @param $data
* @param $class_name
* @param $options
* @param $ns
* @param $is_prefix
* @param $data
* @param $class_name
* @param $options
* @param $ns
* @param $is_prefix
*/
public static function decodeXml()
{

View File

@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
@@ -9,11 +11,9 @@ class Encoder
*
* Wrap json_encode() to throw error when the value being encoded fails.
*
* @access public
* @param $value
* @param $options
* @param $depth
*
* @param $value
* @param $options
* @param $depth
* @return string
* @throws \ErrorException
*/

View File

@@ -1,11 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
use Curl\ArrayUtil;
use Curl\BaseCurl;
use Curl\Url;
class MultiCurl extends BaseCurl
{
public $baseUrl = null;
@@ -44,8 +42,7 @@ class MultiCurl extends BaseCurl
/**
* Construct
*
* @access public
* @param $base_url
* @param $base_url
*/
public function __construct($base_url = null)
{
@@ -60,11 +57,9 @@ class MultiCurl extends BaseCurl
/**
* Add Delete
*
* @access public
* @param $url
* @param $query_parameters
* @param $data
*
* @param $url
* @param $query_parameters
* @param $data
* @return object
*/
public function addDelete($url, $query_parameters = [], $data = [])
@@ -87,10 +82,8 @@ class MultiCurl extends BaseCurl
/**
* Add Download
*
* @access public
* @param $url
* @param $mixed_filename
*
* @param $url
* @param $mixed_filename
* @return object
*/
public function addDownload($url, $mixed_filename)
@@ -148,10 +141,8 @@ class MultiCurl extends BaseCurl
/**
* Add Get
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addGet($url, $data = [])
@@ -173,10 +164,8 @@ class MultiCurl extends BaseCurl
/**
* Add Head
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addHead($url, $data = [])
@@ -198,10 +187,8 @@ class MultiCurl extends BaseCurl
/**
* Add Options
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addOptions($url, $data = [])
@@ -223,10 +210,8 @@ class MultiCurl extends BaseCurl
/**
* Add Patch
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addPatch($url, $data = [])
@@ -253,13 +238,12 @@ class MultiCurl extends BaseCurl
/**
* Add Post
*
* @access public
* @param $url
* @param $data
* @param $follow_303_with_post
* If true, will cause 303 redirections to be followed using a POST request (default: false).
* Note: Redirections are only followed if the CURLOPT_FOLLOWLOCATION option is set to true.
*
* @param $url
* @param $data
* @param $follow_303_with_post
* If true, will cause 303 redirections to be followed using a POST request
* (default: false). Note: Redirections are only followed if the
* CURLOPT_FOLLOWLOCATION option is set to true.
* @return object
*/
public function addPost($url, $data = '', $follow_303_with_post = false)
@@ -296,10 +280,8 @@ class MultiCurl extends BaseCurl
/**
* Add Put
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addPut($url, $data = [])
@@ -325,10 +307,8 @@ class MultiCurl extends BaseCurl
/**
* Add Search
*
* @access public
* @param $url
* @param $data
*
* @param $url
* @param $data
* @return object
*/
public function addSearch($url, $data = [])
@@ -356,9 +336,7 @@ class MultiCurl extends BaseCurl
*
* Add a Curl instance to the handle queue.
*
* @access public
* @param $curl
*
* @param $curl
* @return object
*/
public function addCurl(Curl $curl)
@@ -369,8 +347,6 @@ class MultiCurl extends BaseCurl
/**
* Close
*
* @access public
*/
public function close()
{
@@ -387,8 +363,7 @@ class MultiCurl extends BaseCurl
/**
* Set Concurrency
*
* @access public
* @param $concurrency
* @param $concurrency
*/
public function setConcurrency($concurrency)
{
@@ -398,9 +373,8 @@ class MultiCurl extends BaseCurl
/**
* Set Cookie
*
* @access public
* @param $key
* @param $value
* @param $key
* @param $value
*/
public function setCookie($key, $value)
{
@@ -410,8 +384,7 @@ class MultiCurl extends BaseCurl
/**
* Set Cookies
*
* @access public
* @param $cookies
* @param $cookies
*/
public function setCookies($cookies)
{
@@ -423,8 +396,7 @@ class MultiCurl extends BaseCurl
/**
* Set Cookie String
*
* @access public
* @param $string
* @param $string
*/
public function setCookieString($string)
{
@@ -434,8 +406,7 @@ class MultiCurl extends BaseCurl
/**
* Set Cookie File
*
* @access public
* @param $cookie_file
* @param $cookie_file
*/
public function setCookieFile($cookie_file)
{
@@ -445,8 +416,7 @@ class MultiCurl extends BaseCurl
/**
* Set Cookie Jar
*
* @access public
* @param $cookie_jar
* @param $cookie_jar
*/
public function setCookieJar($cookie_jar)
{
@@ -458,9 +428,8 @@ class MultiCurl extends BaseCurl
*
* Add extra header to include in the request.
*
* @access public
* @param $key
* @param $value
* @param $key
* @param $value
*/
public function setHeader($key, $value)
{
@@ -473,8 +442,7 @@ class MultiCurl extends BaseCurl
*
* Add extra headers to include in the request.
*
* @access public
* @param $headers
* @param $headers
*/
public function setHeaders($headers)
{
@@ -499,8 +467,7 @@ class MultiCurl extends BaseCurl
/**
* Set JSON Decoder
*
* @access public
* @param $mixed boolean|callable
* @param $mixed boolean|callable
*/
public function setJsonDecoder($mixed)
{
@@ -514,8 +481,7 @@ class MultiCurl extends BaseCurl
/**
* Set XML Decoder
*
* @access public
* @param $mixed boolean|callable
* @param $mixed boolean|callable
*/
public function setXmlDecoder($mixed)
{
@@ -532,9 +498,8 @@ class MultiCurl extends BaseCurl
* Set proxies to tunnel requests through. When set, a random proxy will be
* used for the request.
*
* @access public
* @param $proxies array - A list of HTTP proxies to tunnel requests
* through. May include port number.
* @param $proxies array - A list of HTTP proxies to tunnel requests
* through. May include port number.
*/
public function setProxies($proxies)
{
@@ -544,9 +509,8 @@ class MultiCurl extends BaseCurl
/**
* Set Opt
*
* @access public
* @param $option
* @param $value
* @param $option
* @param $value
*/
public function setOpt($option, $value)
{
@@ -557,8 +521,10 @@ class MultiCurl extends BaseCurl
// unexpectedly changing the request url after is has been specified.
if ($option === CURLOPT_URL) {
foreach ($this->queuedCurls as $curl_id => $curl) {
if (!isset($this->instanceSpecificOptions[$curl_id][$option]) ||
$this->instanceSpecificOptions[$curl_id][$option] === null) {
if (
!isset($this->instanceSpecificOptions[$curl_id][$option]) ||
$this->instanceSpecificOptions[$curl_id][$option] === null
) {
$this->instanceSpecificOptions[$curl_id][$option] = $value;
}
}
@@ -568,8 +534,7 @@ class MultiCurl extends BaseCurl
/**
* Set Opts
*
* @access public
* @param $options
* @param $options
*/
public function setOpts($options)
{
@@ -581,8 +546,7 @@ class MultiCurl extends BaseCurl
/**
* Set Rate Limit
*
* @access public
* @param $rate_limit string (e.g. "60/1m").
* @param $rate_limit string (e.g. "60/1m").
* @throws \UnexpectedValueException
*/
public function setRateLimit($rate_limit)
@@ -641,8 +605,7 @@ class MultiCurl extends BaseCurl
* When using a callable decider, the request will be retried until the
* function returns a value which evaluates to false.
*
* @access public
* @param $mixed
* @param $mixed
*/
public function setRetry($mixed)
{
@@ -652,9 +615,8 @@ class MultiCurl extends BaseCurl
/**
* Set Url
*
* @access public
* @param $url
* @param $mixed_data
* @param $url
* @param $mixed_data
*/
public function setUrl($url, $mixed_data = '')
{
@@ -672,7 +634,6 @@ class MultiCurl extends BaseCurl
/**
* Start
*
* @access public
* @throws \ErrorException
*/
public function start()
@@ -687,7 +648,8 @@ class MultiCurl extends BaseCurl
$this->currentRequestCount = 0;
do {
while (count($this->queuedCurls) &&
while (
count($this->queuedCurls) &&
count($this->activeCurls) < $this->concurrency &&
(!$this->rateLimitEnabled || $this->hasRequestQuota())
) {
@@ -733,8 +695,10 @@ class MultiCurl extends BaseCurl
}
}
while ((is_resource($this->multiCurl) || $this->multiCurl instanceof \CurlMultiHandle) &&
(($info_array = curl_multi_info_read($this->multiCurl)) !== false)) {
while (
(is_resource($this->multiCurl) || $this->multiCurl instanceof \CurlMultiHandle) &&
(($info_array = curl_multi_info_read($this->multiCurl)) !== false)
) {
if ($info_array['msg'] === CURLMSG_DONE) {
foreach ($this->activeCurls as $key => $curl) {
if ($curl->curl === $info_array['handle']) {
@@ -782,8 +746,6 @@ class MultiCurl extends BaseCurl
/**
* Stop
*
* @access public
*/
public function stop()
{
@@ -810,8 +772,7 @@ class MultiCurl extends BaseCurl
*
* Remove extra header previously set using Curl::setHeader().
*
* @access public
* @param $key
* @param $key
*/
public function unsetHeader($key)
{
@@ -820,8 +781,6 @@ class MultiCurl extends BaseCurl
/**
* Set request time accuracy
*
* @access public
*/
public function setRequestTimeAccuracy()
{
@@ -830,8 +789,6 @@ class MultiCurl extends BaseCurl
/**
* Destruct
*
* @access public
*/
public function __destruct()
{
@@ -840,8 +797,6 @@ class MultiCurl extends BaseCurl
/**
* Update Headers
*
* @access private
*/
private function updateHeaders()
{
@@ -853,8 +808,7 @@ class MultiCurl extends BaseCurl
/**
* Queue Handle
*
* @access private
* @param $curl
* @param $curl
*/
private function queueHandle($curl)
{
@@ -863,14 +817,16 @@ class MultiCurl extends BaseCurl
$curl->childOfMultiCurl = true;
$this->queuedCurls[$curl->id] = $curl;
$curl->setHeaders($this->headers);
// Avoid overwriting any existing header.
if ($curl->getOpt(CURLOPT_HTTPHEADER) === null) {
$curl->setHeaders($this->headers);
}
}
/**
* Init Handle
*
* @access private
* @param $curl
* @param $curl
* @throws \ErrorException
*/
private function initHandle()
@@ -888,6 +844,9 @@ class MultiCurl extends BaseCurl
if ($curl->beforeSendCallback === null) {
$curl->beforeSend($this->beforeSendCallback);
}
if ($curl->afterSendCallback === null) {
$curl->afterSend($this->afterSendCallback);
}
if ($curl->successCallback === null) {
$curl->success($this->successCallback);
}
@@ -934,8 +893,6 @@ class MultiCurl extends BaseCurl
*
* Checks if there is any available quota to make additional requests while
* rate limiting is enabled.
*
* @access private
*/
private function hasRequestQuota()
{
@@ -965,8 +922,6 @@ class MultiCurl extends BaseCurl
* Wait Until Request Quota Available
*
* Waits until there is available request quota available based on the rate limit.
*
* @access private
*/
private function waitUntilRequestQuotaAvailable()
{

View File

@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
@@ -43,10 +45,8 @@ class StringUtil
/**
* Return true when $haystack starts with $needle.
*
* @access public
* @param $haystack
* @param $needle
*
* @param $haystack
* @param $needle
* @return bool
*/
public static function startsWith($haystack, $needle)

View File

@@ -1,9 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace Curl;
use Curl\StringUtil;
class Url
{
private $baseUrl = null;
@@ -15,7 +15,7 @@ class Url
$this->relativeUrl = $relative_url;
}
public function __toString() : string
public function __toString(): string
{
return $this->absolutizeUrl();
}
@@ -24,6 +24,8 @@ class Url
* Remove dot segments.
*
* Interpret and remove the special "." and ".." path segments from a referenced path.
*
* @param mixed $input
*/
public static function removeDotSegments($input)
{
@@ -87,10 +89,8 @@ class Url
/**
* Build Url
*
* @access public
* @param $url
* @param $mixed_data
*
* @param $url
* @param $mixed_data
* @return string
*/
public static function buildUrl($url, $mixed_data = '')
@@ -127,29 +127,29 @@ class Url
$target = [];
if (isset($r['scheme'])) {
$target['scheme'] = $r['scheme'];
$target['host'] = isset($r['host']) ? $r['host'] : null;
$target['port'] = isset($r['port']) ? $r['port'] : null;
$target['user'] = isset($r['user']) ? $r['user'] : null;
$target['pass'] = isset($r['pass']) ? $r['pass'] : null;
$target['host'] = $r['host'] ?? null;
$target['port'] = $r['port'] ?? null;
$target['user'] = $r['user'] ?? null;
$target['pass'] = $r['pass'] ?? null;
$target['path'] = isset($r['path']) ? self::removeDotSegments($r['path']) : null;
$target['query'] = isset($r['query']) ? $r['query'] : null;
$target['query'] = $r['query'] ?? null;
} else {
$target['scheme'] = isset($b['scheme']) ? $b['scheme'] : null;
$target['scheme'] = $b['scheme'] ?? null;
if ($r['authorized']) {
$target['host'] = isset($r['host']) ? $r['host'] : null;
$target['port'] = isset($r['port']) ? $r['port'] : null;
$target['user'] = isset($r['user']) ? $r['user'] : null;
$target['pass'] = isset($r['pass']) ? $r['pass'] : null;
$target['host'] = $r['host'] ?? null;
$target['port'] = $r['port'] ?? null;
$target['user'] = $r['user'] ?? null;
$target['pass'] = $r['pass'] ?? null;
$target['path'] = isset($r['path']) ? self::removeDotSegments($r['path']) : null;
$target['query'] = isset($r['query']) ? $r['query'] : null;
$target['query'] = $r['query'] ?? null;
} else {
$target['host'] = isset($b['host']) ? $b['host'] : null;
$target['port'] = isset($b['port']) ? $b['port'] : null;
$target['user'] = isset($b['user']) ? $b['user'] : null;
$target['pass'] = isset($b['pass']) ? $b['pass'] : null;
$target['host'] = $b['host'] ?? null;
$target['port'] = $b['port'] ?? null;
$target['user'] = $b['user'] ?? null;
$target['pass'] = $b['pass'] ?? null;
if (!isset($r['path']) || $r['path'] === '') {
$target['path'] = $b['path'];
$target['query'] = isset($r['query']) ? $r['query'] : (isset($b['query']) ? $b['query'] : null);
$target['query'] = $r['query'] ?? $b['query'] ?? null;
} else {
if (StringUtil::startsWith($r['path'], '/')) {
$target['path'] = self::removeDotSegments($r['path']);
@@ -160,14 +160,14 @@ class Url
}
$target['path'] = self::removeDotSegments($base . '/' . $r['path']);
}
$target['query'] = isset($r['query']) ? $r['query'] : null;
$target['query'] = $r['query'] ?? null;
}
}
}
if ($this->relativeUrl === '') {
$target['fragment'] = isset($b['fragment']) ? $b['fragment'] : null;
$target['fragment'] = $b['fragment'] ?? null;
} else {
$target['fragment'] = isset($r['fragment']) ? $r['fragment'] : null;
$target['fragment'] = $r['fragment'] ?? null;
}
$absolutized_url = $this->unparseUrl($target);
return $absolutized_url;
@@ -177,6 +177,8 @@ class Url
* Parse url.
*
* Parse url into components of a URI as specified by RFC 3986.
*
* @param mixed $url
*/
public static function parseUrl($url)
{
@@ -192,6 +194,8 @@ class Url
*
* Percent-encode characters to represent a data octet in a component when
* that octet's corresponding character is outside the allowed set.
*
* @param mixed $chars
*/
private static function percentEncodeChars($chars)
{
@@ -229,16 +233,18 @@ class Url
* Unparse url.
*
* Combine url components into a url.
*
* @param mixed $parsed_url
*/
private function unparseUrl($parsed_url)
{
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$user = $parsed_url['user'] ?? '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? $pass . '@' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$host = $parsed_url['host'] ?? '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$path = $parsed_url['path'] ?? '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
$unparsed_url = $scheme . $user . $pass . $host . $port . $path . $query . $fragment;