Created
November 13, 2016 11:47
-
-
Save asika32764/61b3b1ecb753718c098fb2343ee6774f to your computer and use it in GitHub Desktop.
This file contains 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
use Windwalker\Uri\Uri; | |
/** | |
* The AsyncRequest class. | |
* | |
* @since {DEPLOY_VERSION} | |
*/ | |
class AsyncRequest | |
{ | |
/** | |
* Property requests. | |
* | |
* @var array | |
*/ | |
protected $requests = []; | |
/** | |
* createHandle | |
* | |
* @param string $method | |
* @param string $url | |
* @param string $data | |
* | |
* @return resource | |
*/ | |
public static function createHandle($method, $url, $data = '') | |
{ | |
$curlHandle = curl_init($url); | |
$defaultOptions = array( | |
CURLOPT_CUSTOMREQUEST => strtoupper($method), | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HEADER => true, | |
CURLOPT_POSTFIELDS => $data, | |
CURLOPT_SSL_VERIFYHOST => false, | |
CURLOPT_SSL_VERIFYPEER => false | |
); | |
curl_setopt_array($curlHandle, $defaultOptions); | |
return $curlHandle; | |
} | |
/** | |
* addRequest | |
* | |
* @param string $method | |
* @param string $url | |
* @param string $data | |
* | |
* @return static | |
*/ | |
public function addRequest($method, $url, $data = '') | |
{ | |
$this->requests[] = static::createHandle($method, $url, $data); | |
return $this; | |
} | |
/** | |
* get | |
* | |
* @param string $url | |
* @param array $data | |
* | |
* @return static | |
*/ | |
public function get($url, array $data = []) | |
{ | |
$uri = new Uri($url); | |
foreach ($data as $k => $value) | |
{ | |
$uri->setVar($k, $value); | |
} | |
return $this->addRequest('GET', $uri->toString()); | |
} | |
/** | |
* post | |
* | |
* @param string $url | |
* @param string $data | |
* | |
* @return static | |
*/ | |
public function post($url, $data) | |
{ | |
return $this->addRequest('POST', $url, $data); | |
} | |
/** | |
* put | |
* | |
* @param string $url | |
* @param string $data | |
* | |
* @return static | |
*/ | |
public function put($url, $data) | |
{ | |
return $this->addRequest('PUT', $url, $data); | |
} | |
/** | |
* patch | |
* | |
* @param string $url | |
* @param string $data | |
* | |
* @return static | |
*/ | |
public function patch($url, $data) | |
{ | |
return $this->addRequest('PATCH', $url, $data); | |
} | |
/** | |
* delete | |
* | |
* @param string $url | |
* @param string $data | |
* | |
* @return static | |
*/ | |
public function delete($url, $data) | |
{ | |
return $this->addRequest('DELETE', $url, $data); | |
} | |
/** | |
* requests | |
* | |
* @return array | |
*/ | |
public function request() | |
{ | |
$curlMultiHandle = curl_multi_init(); | |
$responses = array(); | |
$running = null; | |
foreach ($this->requests as $request) | |
{ | |
curl_multi_add_handle($curlMultiHandle, $request); | |
} | |
do | |
{ | |
curl_multi_exec($curlMultiHandle, $running); | |
} | |
while ($running > 0); | |
foreach ($this->requests as $id => $handle) | |
{ | |
$responses[$id] = curl_multi_getcontent($handle); | |
curl_multi_remove_handle($curlMultiHandle, $handle); | |
} | |
curl_multi_close($curlMultiHandle); | |
return $responses; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment