Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ######### RollingCurl_old : https://github.com/LionsAd/rolling-curl
- $this->urlsArray = [
- 'x'=>['url'=>'http://www.google.com', 'post'=>[] ],
- 'b'=>['url'=>'http://www.facebook.com', 'post'=>[] ],
- 'l'=>['url'=>'http://www.yahoo.com', 'post'=>[] ]
- ];
- public function request_callback($response, $info, $request) {
- $this->var_dump($response);
- $original_url =$request->url;
- $original_key =$request->headers['url_key'];
- $this->var_dump($info['url']);
- if ($info['http_code'] ==200)...
- echo '<hr>';
- }
- $helpers->multi_curl_rolling_old( $this->urlsArray, [$this,'request_callback']);
- $info:
- 'url' => string 'http://www.google.com/' (length=22)
- 'content_type' => string 'text/html; charset=ISO-8859-1' (length=29)
- 'http_code' => int 200
- 'total_time' => float 0.212069
- 'redirect_url' => string '' (length=0)
- */
- public $multi_curl_rolling_old_arrs=[];
- public function multi_curl_rolling_old($urlsArray, $opts=[], $callback=null, $decode=true)
- {
- $uniqId = uniqid('k');
- $this->multi_curl_rolling_old_arrs[$uniqId]=[];
- $callback_final = function($response, $info, $request) use($callback, $uniqId, $decode){
- $original_url =$request->url;
- $original_key =$request->headers['url_key'];
- if ($info['http_code'] ==200){
- }
- try{ $this->multi_curl_rolling_old_arrs[$uniqId][$original_key]= $decode ? json_decode($response) : $response; }
- catch(\Exception $ex) { $this->multi_curl_rolling_old_arrs[$uniqId][$original_key]= $response; }
- call_user_func( ($func = $callback==null ? function(){} : $callback), $response, $info, $request);
- };
- $rc = new \Puvox\RollingCurl_old($callback_final);
- // $rc->options = array(CURLOPT_HEADER => true, CURLOPT_NOBODY => true);
- $simultaneous_urls = empty($opts['simultaneous_urls']) ? 10 : $opts['simultaneous_urls'];
- if (!empty($opts['headers'])) { $headers=[]; foreach($opts['headers'] as $key=>$value) { $headers[]="$key:$value"; } $rc->__set('headers',$headers ); }
- $rc->window_size = $simultaneous_urls; // the window size determines how many simultaneous requests to allow.
- //if bare urls array or multi-dimensional
- if (is_string($urlsArray[ array_keys($urlsArray)[0] ])){
- foreach($urlsArray as $key=>$each){
- $final_array[$key]=['url'=>$each];
- }
- }
- else{
- $final_array =$urlsArray;
- }
- foreach ($final_array as $key=>$urlBlock) {
- $url = $urlBlock['url'];
- $postArray = empty($urlBlock['post'])? [] : $urlBlock['post'];
- $request = empty($postArray) ? new \Puvox\RollingCurlRequest_old($url) : new \Puvox\RollingCurlRequest_old($url, 'POST', $postArray);
- $request->headers['url_key']=$key;
- // $request->options = array(CURLOPT_HEADER => true, CURLOPT_NOBODY => true);
- $rc->add($request); // add each request to the RollingCurl object
- }
- $rc->execute();
- $ress = $this->multi_curl_rolling_old_arrs[$uniqId];
- unset($this->multi_curl_rolling_old_arrs[$uniqId]);
- return $ress;
- }
- // https://github.com/joshfraser/rolling-curl
- class RollingCurlRequest_old {
- public $url = false;
- public $method = 'GET';
- public $post_data = null;
- public $headers = null;
- public $options = null;
- function __construct($url, $method = "GET", $post_data = null, $headers = null, $options = null) {
- $this->url = $url;
- $this->method = $method;
- $this->post_data = $post_data;
- $this->headers = $headers;
- $this->options = $options;
- }
- public function __destruct() {
- unset($this->url, $this->method, $this->post_data, $this->headers, $this->options);
- }
- }
- class RollingCurlException_old extends \Exception {}
- class RollingCurl_old {
- private $window_size = 5;
- private $timeout = 10;
- private $callback;
- protected $options = array(CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_AUTOREFERER => true, CURLOPT_ENCODING => '', CURLOPT_TIMEOUT => 30);
- private $headers = array();
- private $requests = array();
- private $requestMap = array();
- function __construct($callback = null) {
- $this->callback = $callback;
- }
- public function __get($name) {
- return (isset($this->{
- $name
- })) ? $this->{
- $name
- }
- : null;
- }
- public function __set($name, $value) {
- if ($name == "options" || $name == "headers") {
- $this->{
- $name
- } = $value + $this->{
- $name
- };
- } else {
- $this->{
- $name
- } = $value;
- }
- return true;
- }
- public function add($request) {
- $this->requests[] = $request;
- return true;
- }
- public function request($url, $method = "GET", $post_data = null, $headers = null, $options = null) {
- $this->requests[] = new RollingCurlRequest_old($url, $method, $post_data, $headers, $options);
- return true;
- }
- public function get($url, $headers = null, $options = null) {
- return $this->request($url, "GET", null, $headers, $options);
- }
- public function post($url, $post_data = null, $headers = null, $options = null) {
- return $this->request($url, "POST", $post_data, $headers, $options);
- }
- public function IsNewPhp() {
- return PHP_MAJOR_VERSION >= 8;
- }
- public function HandleKey($curlHandle) {
- return $this->IsNewPhp() ? curl_getinfo($curlHandle, CURLINFO_PRIVATE) : (string)$curlHandle;
- }
- public function execute($window_size = null) {
- if (sizeof($this->requests) == 1) {
- return $this->single_curl();
- } else {
- return $this->rolling_curl($window_size);
- }
- }
- private function single_curl() {
- $ch = curl_init();
- $request = array_shift($this->requests);
- $options = $this->get_options($request);
- curl_setopt_array($ch, $options);
- $output = curl_exec($ch);
- $info = curl_getinfo($ch);
- if ($this->callback) {
- $callback = $this->callback;
- if (is_callable($this->callback)) {
- call_user_func($callback, $output, $info, $request);
- }
- } else
- return $output;
- return true;
- }
- private function rolling_curl($window_size = null) {
- if ($window_size)
- $this->window_size = $window_size;
- if (sizeof($this->requests) < $this->window_size)
- $this->window_size = sizeof($this->requests);
- if ($this->window_size < 2) {
- throw new RollingCurlException_old("Window size must be greater than 1");
- }
- $master = curl_multi_init();
- for ($i = 0; $i < $this->window_size; $i++) {
- $ch = curl_init();
- $options = $this->get_options($this->requests[$i]);
- if ($this->IsNewPhp())
- $options[CURLOPT_PRIVATE] = "req_$i";
- curl_setopt_array($ch, $options);
- curl_multi_add_handle($master, $ch);
- $key = $this->HandleKey($ch);
- $this->requestMap[$key] = $i;
- }
- do {
- while (($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
- if ($execrun != CURLM_OK) {
- break;
- }
- while ($done = curl_multi_info_read($master)) {
- $info = curl_getinfo($done['handle']);
- $output = curl_multi_getcontent($done['handle']);
- $callback = $this->callback;
- if (is_callable($callback)) {
- $key = $this->HandleKey($done['handle']);
- $request = $this->requests[$this->requestMap[$key]];
- unset($this->requestMap[$key]);
- call_user_func($callback, $output, $info, $request);
- }
- if ($i < sizeof($this->requests) && isset($this->requests[$i]) && $i < count($this->requests)) {
- $ch = curl_init();
- $options = $this->get_options($this->requests[$i]);
- if ($this->IsNewPhp())
- $options[CURLOPT_PRIVATE] = "req_$i";
- curl_setopt_array($ch, $options);
- curl_multi_add_handle($master, $ch);
- $key = $this->HandleKey($ch);
- $this->requestMap[$key] = $i;
- $i++;
- }
- curl_multi_remove_handle($master, $done['handle']);
- }
- if ($running) {
- curl_multi_select($master, $this->timeout);
- }
- } while ($running);
- curl_multi_close($master);
- return true;
- }
- private function get_options($request) {
- $options = $this->__get('options');
- if ((ini_get('safe_mode') == 'Off' || !ini_get('safe_mode')) && ini_get('open_basedir') == '') {
- $options[CURLOPT_FOLLOWLOCATION] = 1;
- $options[CURLOPT_MAXREDIRS] = 5;
- }
- $headers = $this->__get('headers');
- if ($request->options) {
- $options = $request->options + $options;
- }
- $options[CURLOPT_URL] = $request->url;
- if ($request->post_data) {
- $options[CURLOPT_POST] = 1;
- $options[CURLOPT_POSTFIELDS] = $request->post_data;
- }
- if ($headers) {
- $options[CURLOPT_HEADER] = 0;
- $options[CURLOPT_HTTPHEADER] = $headers;
- }
- if (!empty($options[CURLOPT_WRITEFUNCTION])) {
- $writeCallback = $options[CURLOPT_WRITEFUNCTION];
- unset($options[CURLOPT_WRITEFUNCTION]);
- $options[CURLOPT_WRITEFUNCTION] = $writeCallback;
- }
- return $options;
- }
- public function __destruct() {
- unset($this->window_size, $this->callback, $this->options, $this->headers, $this->requests);
- }
- }
Add Comment
Please, Sign In to add comment