function addMembers($auth_code, $id, $pilots)
{
    $count = count($pilots);
    $i = 1;
    $j = 0;
    foreach ($pilots as $pilot) {
        //	echo $i."/".$count." ".$pilot->name."<br>";
        if ($j == 0) {
            $mh = curl_multi_init();
            $mhArray = [];
        }
        $mhArray[$j] = curl_multi_post_handle("https://crest-tq.eveonline.com/characters/" . $id . "/contacts/", $auth_code, (int) $pilot->character_id, $pilot->name, $_GET["standing"], $_GET["watched"], $_GET["blocked"]);
        curl_multi_add_handle($mh, $mhArray[$j]);
        if ($j == 19) {
            $j = 0;
            do {
                curl_multi_exec($mh, $running);
                curl_multi_select($mh);
            } while ($running > 0);
            curl_multi_close($mh);
        } else {
            $j = $j + 1;
        }
        //print_r( curl_post("https://crest-tq.eveonline.com/characters/".$id."/contacts/",$auth_code,(int)$pilot->character_id,$pilot->name));
        $i = $i + 1;
        flush();
    }
    do {
        curl_multi_exec($mh, $running);
        curl_multi_select($mh);
    } while ($running > 0);
    curl_multi_close($mh);
    echo $count;
}
 private function checkForCompletedRequests()
 {
     do {
         $mrc = curl_multi_exec($this->multi_handle, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($this->multi_handle) != -1) {
             do {
                 $mrc = curl_multi_exec($this->multi_handle, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         } else {
             return;
         }
     }
     // Now grab the information about the completed requests
     while ($info = curl_multi_info_read($this->multi_handle)) {
         $ch = $info['handle'];
         $ch_array_key = (int) $ch;
         if (!isset($this->outstanding_requests[$ch_array_key])) {
             die("Error - handle wasn't found in requests: '{$ch}' in " . print_r($this->outstanding_requests, true));
         }
         $request = $this->outstanding_requests[$ch_array_key];
         $url = $request['url'];
         $content = curl_multi_getcontent($ch);
         $callback = $request['callback'];
         $user_data = $request['user_data'];
         call_user_func($callback, $content, $url, $ch, $user_data);
         unset($this->outstanding_requests[$ch_array_key]);
         curl_multi_remove_handle($this->multi_handle, $ch);
     }
 }
Esempio n. 3
0
 public function flush()
 {
     foreach ($this->queue as $i => $queue) {
         list($request, $response, $curl) = $queue;
         if (null === $curl) {
             $curl = $this->queue[$i][2] = static::createCurlHandle();
         }
         $this->prepare($request, $response, $curl);
         curl_multi_add_handle($this->curl, $curl);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($this->curl, $active);
     } while (CURLM_CALL_MULTI_PERFORM == $mrc);
     while ($active && CURLM_OK == $mrc) {
         if (-1 != curl_multi_select($this->curl)) {
             do {
                 $mrc = curl_multi_exec($this->curl, $active);
             } while (CURLM_CALL_MULTI_PERFORM == $mrc);
         }
     }
     foreach ($this->queue as $queue) {
         list($request, $response, $curl) = $queue;
         $response->fromString(static::getLastResponse(curl_multi_getcontent($curl)));
         curl_multi_remove_handle($this->curl, $curl);
     }
     $this->queue = array();
 }
 public function multiCurlSelect()
 {
     // TODO: Yes, very very dirty. cURL integration with event loop is required
     $timeout = 0.01;
     curl_multi_select($this->curl_mh, $timeout);
     $this->multiCurlPoll();
 }
Esempio n. 5
0
 private function execSubrequests()
 {
     do {
         curl_multi_exec($this->multiDescriptor, $active);
         curl_multi_select($this->multiDescriptor);
     } while ($active > 0);
 }
 function exec($one = null)
 {
     if (!($this->mh && $this->ch)) {
         return false;
     }
     $running = null;
     do {
         curl_multi_exec($this->mh, $running);
         curl_multi_select($this->mh);
     } while ($running > 0);
     foreach ($this->ch as $key => $value) {
         $result[$key] = curl_multi_getcontent($value);
         curl_multi_remove_handle($this->mh, $value);
         curl_close($value);
     }
     curl_multi_close($this->mh);
     $this->mh = $this->ch = null;
     if ($one) {
         $out = null;
         foreach ($result as $key => $value) {
             $out .= $value;
         }
         return $out;
     } else {
         return $result;
     }
 }
Esempio n. 7
0
 /**
  * 並列リクエストを実行します。
  * 
  * @param CurlMultiHandler $cmh
  * @return array[CurlResponse]
  */
 public static function multieFecth(CurlMultiHandler $cmh)
 {
     $mh = $cmh->getHandle();
     $active = null;
     do {
         $mrc = curl_multi_exec($mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active and $mrc == CURLM_OK) {
         if (curl_multi_select($mh) != -1) {
             do {
                 $mrc = curl_multi_exec($mh, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     $crs = array();
     foreach ($cmh->getHandlers() as $h) {
         $ch = $h->getHandle();
         $response = curl_multi_getcontent($ch);
         if ($response === false) {
             throw new CurlFetchException(curl_errno($ch), curl_error($ch));
         }
         array_push($crs, new CurlResponse($response, $ch));
         $cmh->remove($h);
         $h->close();
     }
     $cmh = null;
     return $crs;
 }
Esempio n. 8
0
 /** Execute request or get its response
  * @param string request identifier
  * @param array array(string $url) for executing request, array() for getting response
  * @return mixed
  */
 function __call($name, array $args)
 {
     if ($args) {
         // execute request
         list($url) = $args;
         $curl = curl_init($url);
         $this->curl[$name] = $curl;
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         $return = curl_multi_add_handle($this->multi, $curl);
         while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
         }
         return $return;
     }
     // get response
     if (!isset($this->curl[$name])) {
         // wrong identifier
         return false;
     }
     $curl = $this->curl[$name];
     while (!isset($this->done[(int) $curl])) {
         curl_multi_select($this->multi, $this->timeout);
         while (curl_multi_exec($this->multi, $running) == CURLM_CALL_MULTI_PERFORM) {
         }
         while ($info = curl_multi_info_read($this->multi)) {
             if ($info["msg"] == CURLMSG_DONE) {
                 $this->done[(int) $info["handle"]] = true;
             }
         }
     }
     return curl_multi_getcontent($curl);
 }
Esempio n. 9
0
function main()
{
    $port = $_ENV['SERVERPORT'];
    $host = php_uname('n');
    $url = "http://{$host}:{$port}/hello.php";
    $ch1 = curl_init();
    $ch2 = curl_init();
    curl_setopt($ch1, CURLOPT_URL, $url);
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch1, CURLOPT_WRITEFUNCTION, 'except');
    curl_setopt($ch2, CURLOPT_URL, $url);
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    $mh = curl_multi_init();
    curl_multi_add_handle($mh, $ch1);
    curl_multi_add_handle($mh, $ch2);
    $active = null;
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    curl_multi_close($mh);
}
Esempio n. 10
0
 public function exec()
 {
     $active = null;
     do {
         do {
             $status = curl_multi_exec($this->curlHandle, $active);
         } while ($status == CURLM_CALL_MULTI_PERFORM);
         if ($status != CURLM_OK) {
             break;
         }
         $response = array();
         $respond = curl_multi_info_read($this->curlHandle);
         while ($respond) {
             $callback = $this->requestMap[(string) $respond['handle']];
             $responses[$callback]['content'] = curl_multi_getcontent($respond['handle']);
             $responses[$callback]['httpcode'] = curl_getinfo($respond['handle'], CURLINFO_HTTP_CODE);
             curl_multi_remove_handle($this->curlHandle, $respond['handle']);
             curl_close($respond['handle']);
             $respond = curl_multi_info_read($this->curlHandle);
         }
         if ($active > 0) {
             curl_multi_select($this->curlHandle, 0.05);
         }
     } while ($active);
     return $responses;
 }
Esempio n. 11
0
 private function consumeStream()
 {
     $url = 'https://stream.gnip.com/accounts/' . GNIP_ACCOUNT . '/publishers/' . $this->endpoint . '/streams/track/Prod.json';
     $that = $this;
     $callback = function ($ch, $data) use($that) {
         $chunk = trim($data);
         if (!empty($chunk)) {
             $that->handleChunk($chunk);
         }
         return strlen($data);
     };
     $ch = curl_init();
     curl_setopt_array($ch, array(CURLOPT_URL => $url, CURLOPT_ENCODING => 'gzip', CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => GNIP_USERNAME . ':' . GNIP_PASSWORD, CURLOPT_WRITEFUNCTION => $callback, CURLOPT_BUFFERSIZE => 2000, CURLOPT_LOW_SPEED_LIMIT => 1, CURLOPT_LOW_SPEED_TIME => 60));
     $running = null;
     $mh = curl_multi_init();
     curl_multi_add_handle($mh, $ch);
     // the event loop
     do {
         curl_multi_select($mh, 1);
         // wait for activity
         curl_multi_exec($mh, $running);
         // perform activity
     } while ($running > 0);
     curl_multi_remove_handle($mh, $ch);
     curl_multi_close($mh);
 }
 /**
  * Get the size of each of the urls in a list
  *
  * @param array $urls
  *
  * @return array
  * @throws \Exception
  */
 public function batch(array $urls)
 {
     $multi = curl_multi_init();
     $results = array();
     // Create the curl handles and add them to the multi_request
     foreach (array_values($urls) as $count => $uri) {
         $results[$uri] = array();
         ${$count} = $this->handle($uri, $results[$uri]);
         $code = curl_multi_add_handle($multi, ${$count});
         if ($code != CURLM_OK) {
             throw new \Exception("Curl handle for {$uri} could not be added");
         }
     }
     // Perform the requests
     do {
         while (($mrc = curl_multi_exec($multi, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($mrc != CURLM_OK && $mrc != CURLM_CALL_MULTI_PERFORM) {
             throw new \Exception("Curl error code: {$mrc}");
         }
         if ($active && curl_multi_select($multi) === -1) {
             // Perform a usleep if a select returns -1.
             // See: https://bugs.php.net/bug.php?id=61141
             usleep(250);
         }
     } while ($active);
     // Figure out why individual requests may have failed
     foreach (array_values($urls) as $count => $uri) {
         $error = curl_error(${$count});
         if ($error) {
             $results[$uri]['failure_reason'] = $error;
         }
     }
     return $results;
 }
Esempio n. 13
0
 public function run()
 {
     // if sth. goes wrong with init (no requests), return false.
     if (!($mh = $this->initMultiHandle())) {
         return false;
     }
     $active = 0;
     do {
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while (CURLM_CALL_MULTI_PERFORM === $mrc);
         switch ($mrc) {
             case CURLM_OK:
                 break;
             case CURLM_OUT_OF_MEMORY:
                 die('CURL out of memory.');
                 break;
             case CURLM_INTERNAL_ERROR:
                 ezcLog::getInstance()->log('CURL_INTERNAL ERROR', ezcLog::FATAL);
                 break;
         }
         // Did sth. happen? Did a handle finish?
         $moreMessages = 0;
         do {
             $this->handleMultiMessage(curl_multi_info_read($mh, $moreMessages));
         } while ($moreMessages);
         // wait for sth. to do
         if (-1 === curl_multi_select($mh)) {
             ezcLog::getInstance()->log('curl_multi_select returned -1', ezcLog::FATAL);
             $active = false;
             // break the loop
         }
     } while ($active);
     return TRUE;
 }
Esempio n. 14
0
function multihttpget($urls)
{
    $mh = curl_multi_init();
    foreach ($urls as $i => $oneurl) {
        $ch[$i] = curl_init();
        curl_setopt($ch[$i], CURLOPT_URL, $oneurl);
        curl_setopt($ch[$i], CURLOPT_HEADER, 0);
        curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch[$i], CURLOPT_TIMEOUT, 100);
        $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
        curl_setopt($ch[$i], CURLOPT_USERAGENT, $useragent);
        curl_multi_add_handle($mh, $ch[$i]);
    }
    $active = null;
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    $response = array();
    foreach ($ch as $k => $h) {
        $response[] = curl_multi_getcontent($h);
    }
    foreach ($ch as $h) {
        curl_multi_remove_handle($mh, $h);
    }
    curl_multi_close($mh);
    return $response;
}
Esempio n. 15
0
 public function getResult($key = NULL)
 {
     if ($key != NULL) {
         if (isset($this->responses[$key])) {
             return $this->responses[$key];
         }
         $innerSleepInt = $outerSleepInt = 1;
         while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) {
             usleep(intval($outerSleepInt));
             $outerSleepInt = intval(max(1, $outerSleepInt * $this->sleepIncrement));
             $ms = curl_multi_select($this->mc, 0);
             if ($ms > 0) {
                 do {
                     $this->execStatus = curl_multi_exec($this->mc, $this->running);
                     usleep(intval($innerSleepInt));
                     $innerSleepInt = intval(max(1, $innerSleepInt * $this->sleepIncrement));
                 } while ($this->execStatus == CURLM_CALL_MULTI_PERFORM);
                 $innerSleepInt = 1;
             }
             $this->storeResponses();
             if (isset($this->responses[$key]['data'])) {
                 return $this->responses[$key];
             }
             $runningCurrent = $this->running;
         }
         return NULL;
     }
     return FALSE;
 }
Esempio n. 16
0
 /**
  * Get the size of each of the urls in a list
  *
  * @param array $urls
  *
  * @return array
  * @throws \Exception
  */
 public function batch(array $urls)
 {
     $multi = curl_multi_init();
     $results = array();
     foreach (array_values($urls) as $count => $uri) {
         $results[$uri] = [];
         ${$count} = $this->handle($uri, $results[$uri]);
         $code = curl_multi_add_handle($multi, ${$count});
         if ($code != CURLM_OK) {
             throw new \Exception("Curl handle for {$uri} could not be added");
         }
     }
     do {
         while (($mrc = curl_multi_exec($multi, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($mrc != CURLM_OK && $mrc != CURLM_CALL_MULTI_PERFORM) {
             throw new \Exception("Curl error code: {$mrc}");
         }
         if ($active && curl_multi_select($multi) === -1) {
             // Perform a usleep if a select returns -1.
             // See: https://bugs.php.net/bug.php?id=61141
             usleep(250);
         }
     } while ($active);
     return $results;
 }
Esempio n. 17
0
 public function execute()
 {
     $mh = curl_multi_init();
     $chs = $this->chs();
     $stillRunning = 0;
     $result = array();
     if (function_exists('curl_multi_setopt')) {
         curl_multi_setopt($mh, CURLMOPT_PIPELINING, 1);
         curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, $this->maxConnectsSize());
     }
     foreach ($chs as $ch) {
         curl_multi_add_handle($mh, $ch);
     }
     do {
         $execReturnValue = curl_multi_exec($mh, $stillRunning);
         curl_multi_select($mh);
     } while ($stillRunning > 0);
     foreach ($chs as $i => $ch) {
         $curlError = curl_error($ch);
         if ($curlError === "") {
             $result[$i] = curl_multi_getcontent($ch);
         } else {
             throw new PubnubException("Curl error on handle {$i}: {$curlError}\n");
         }
         curl_multi_remove_handle($mh, $ch);
         curl_close($ch);
     }
     curl_multi_close($mh);
     $this->requests = array();
     if ($execReturnValue != CURLM_OK) {
         throw new PubnubException(curl_multi_strerror($execReturnValue));
     }
     return $result;
 }
Esempio n. 18
0
 /**
  * @param $mh
  * @param array $results
  * @param array $options
  * @return int
  */
 public static function makeRequests($mh, array &$results, array $options = array())
 {
     // While we're still active, execute curl
     $active = NULL;
     do {
         // Continue to exec until curl is ready to
         // give us more data
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         // Wait for activity on any curl-connection
         curl_multi_select($mh);
         //do something with the return values
         while (($info = curl_multi_info_read($mh)) !== FALSE) {
             if ($info["result"] == CURLE_OK) {
                 $info['content'] = curl_multi_getcontent($info["handle"]);
             } else {
                 $info['error'] = curl_error($info["handle"]);
             }
             $results[strval($info["handle"])] = $info;
             curl_multi_remove_handle($mh, $info["handle"]);
         }
     } while ($active && $mrc == CURLM_OK);
     curl_multi_close($mh);
     return $mrc;
 }
Esempio n. 19
0
 public function bFunction()
 {
     // create both cURL resources
     $ch1 = new Curl();
     $ch2 = new Curl();
     // set URL and other appropriate options
     $ch1->setUrl("http://www.example.com/")->includeHeader(false);
     $ch2->setUrl("http://www.php.net/")->includeHeader(false);
     //create the multiple cURL handle
     $mh = new MultiCurl();
     //add the two handles
     $mh->add($ch1)->add($ch2);
     $active = null;
     // execute the handles
     do {
         $mrc = curl_multi_exec($mh->getMultiHandle(), $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         $s = curl_multi_select($mh->getMultiHandle());
         if ($s != -1) {
             do {
                 $mrc = curl_multi_exec($mh->getMultiHandle(), $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     //close the handles
     unset($mh);
 }
Esempio n. 20
0
 public function flush()
 {
     if (false === ($curlm = curl_multi_init())) {
         throw new ClientException('Unable to create a new cURL multi handle');
     }
     // prepare a cURL handle for each entry in the queue
     foreach ($this->queue as $i => &$queue) {
         list($request, $response, $options) = $queue;
         $curl = $queue[] = static::createCurlHandle();
         $this->prepare($curl, $request, $options);
         curl_multi_add_handle($curlm, $curl);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($curlm, $active);
     } while (CURLM_CALL_MULTI_PERFORM == $mrc);
     while ($active && CURLM_OK == $mrc) {
         if (-1 != curl_multi_select($curlm)) {
             do {
                 $mrc = curl_multi_exec($curlm, $active);
             } while (CURLM_CALL_MULTI_PERFORM == $mrc);
         }
     }
     // populate the responses
     while (list($request, $response, $options, $curl) = array_shift($this->queue)) {
         static::populateResponse($curl, curl_multi_getcontent($curl), $response);
         curl_multi_remove_handle($curlm, $curl);
     }
     curl_multi_close($curlm);
 }
Esempio n. 21
0
 function _fetch_multi($urls = array())
 {
     /** multi curl handler */
     $curl_handler = array();
     $multi_curl_handler = curl_multi_init();
     foreach ($urls as $sha => $url) {
         $curl_handler[$sha] = curl_init();
         curl_setopt_array($curl_handler[$sha], $this->_get_curlopt($url));
         curl_multi_add_handle($multi_curl_handler, $curl_handler[$sha]);
     }
     do {
         $execReturnValue = curl_multi_exec($multi_curl_handler, $runningHandles);
     } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
     while ($runningHandles && $execReturnValue == CURLM_OK) {
         $numberReady = curl_multi_select($multi_curl_handler);
         if ($numberReady != -1) {
             do {
                 $execReturnValue = curl_multi_exec($multi_curl_handler, $runningHandles);
             } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
         }
     }
     $response_content = array();
     foreach ($urls as $sha => $url) {
         if (curl_error($curl_handler[$sha]) == '') {
             $response_content[$sha] = curl_multi_getcontent($curl_handler[$sha]);
         }
         curl_multi_remove_handle($multi_curl_handler, $curl_handler[$sha]);
         curl_close($curl_handler[$sha]);
     }
     curl_multi_close($multi_curl_handler);
     return $response_content;
 }
Esempio n. 22
0
 public function Run($echo = true)
 {
     if (!is_array($this->threads)) {
         return false;
     }
     $session = serialize($_SESSION);
     session_write_close();
     //Start
     $cmh = curl_multi_init();
     $tasks = array();
     foreach ($this->threads as $i => $thread) {
         $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('PHPThreads: true'));
         curl_setopt($ch, CURLOPT_POST, 1);
         $Post = array('PHPThreads_Run' => base64_encode($this->strcode($thread[0], $this->password)), 'PHPThreads_Vars' => base64_encode($this->strcode($thread[1], $this->password)), 'PHPThreads_Session' => base64_encode($this->strcode($session, $this->password)));
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Post));
         $tasks[$i] = $ch;
         curl_multi_add_handle($cmh, $ch);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($cmh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($cmh) != -1) {
             do {
                 $mrc = curl_multi_exec($cmh, $active);
                 $info = curl_multi_info_read($cmh);
                 if ($info['msg'] == CURLMSG_DONE) {
                     $ch = $info['handle'];
                     $url = array_search($ch, $tasks);
                     $result = curl_multi_getcontent($ch);
                     $curl_result = json_decode($result, true);
                     if ($echo) {
                         echo $curl_result['echo'];
                     }
                     $resp[$url] = $curl_result['return'];
                     curl_multi_remove_handle($cmh, $ch);
                     curl_close($ch);
                 }
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     curl_multi_close($cmh);
     session_start();
     $this->Clear();
     //Clear Threads after run
     if (is_array($resp)) {
         ksort($resp);
     }
     return $resp;
     // End
 }
Esempio n. 23
0
 public function getResult($key = null)
 {
     if ($key != null) {
         if (isset($this->responses[$key]['data'])) {
             return $this->responses[$key];
         }
         $innerSleepInt = $outerSleepInt = 1;
         while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) {
             usleep($outerSleepInt);
             $outerSleepInt *= $this->sleepIncrement;
             $ms = curl_multi_select($this->mc);
             if ($ms >= CURLM_CALL_MULTI_PERFORM) {
                 do {
                     $this->execStatus = curl_multi_exec($this->mc, $this->running);
                     usleep($innerSleepInt);
                     $innerSleepInt *= $this->sleepIncrement;
                 } while ($this->execStatus == CURLM_CALL_MULTI_PERFORM);
                 $innerSleepInt = 1;
             }
             $this->storeResponses();
             if (isset($this->responses[$key]['data'])) {
                 return $this->responses[$key];
             }
             $runningCurrent = $this->running;
         }
         return null;
     }
     return false;
 }
Esempio n. 24
0
 public function execute()
 {
     $mh = curl_multi_init();
     $conn = [];
     foreach ($this->getUrls() as $k => $url) {
         $this->setOption(CURLOPT_URL, $url);
         $ch = curl_init();
         curl_setopt_array($ch, $this->getOptions());
         $conn[$k] = $ch;
         curl_multi_add_handle($mh, $conn[$k]);
     }
     $active = false;
     do {
         $mrc = curl_multi_exec($mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($mh) != -1) {
             do {
                 $mrc = curl_multi_exec($mh, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     $res = [];
     foreach ($this->getUrls() as $k => $url) {
         $res[$k] = curl_multi_getcontent($conn[$k]);
         curl_close($conn[$k]);
         curl_multi_remove_handle($mh, $conn[$k]);
     }
     curl_multi_close($mh);
     return $res;
 }
Esempio n. 25
0
 /**
  * Execute the requests of all given Curl instances
  *
  * @param Curl[] $instances An array of Curl instances (key = name of instance)
  * @param int $retryCount The maximum number of retries
  * @param int $retryWait Time in seconds to wait between each try
  */
 private function subExec($instances, $retryCount = 0, $retryWait = 0)
 {
     $curlMultiInstance = curl_multi_init();
     foreach ($instances as $name => $curlInstance) {
         curl_multi_add_handle($curlMultiInstance, $curlInstance->getHandle());
     }
     do {
         curl_multi_exec($curlMultiInstance, $stillRunning);
         curl_multi_select($curlMultiInstance);
     } while ($stillRunning);
     for ($retry = 1; $retry <= $retryCount; $retry++) {
         $retryRequired = false;
         foreach ($instances as $curlInstance) {
             if (!$curlInstance->isSuccessful()) {
                 $retryRequired = true;
                 break;
             }
         }
         if (!$retryRequired) {
             break;
         }
         sleep($retryWait);
         $this->retryFailed($instances);
     }
     foreach ($instances as $curlInstance) {
         $curlInstance->setContent(curl_multi_getcontent($curlInstance->getHandle()));
     }
     curl_multi_close($curlMultiInstance);
 }
Esempio n. 26
0
 public function getResult($key = null)
 {
     if ($key != null) {
         if (isset($this->responses[$key]['code'])) {
             return $this->responses[$key];
         }
         $innerSleepInt = $outerSleepInt = 1;
         while ($this->running && ($this->execStatus == CURLM_OK || $this->execStatus == CURLM_CALL_MULTI_PERFORM)) {
             usleep(intval($outerSleepInt));
             $outerSleepInt = intval(max(1, $outerSleepInt * $this->sleepIncrement));
             $ms = curl_multi_select($this->mc, 0);
             // bug in PHP 5.3.18+ where curl_multi_select can return -1
             // https://bugs.php.net/bug.php?id=63411
             if ($ms === -1) {
                 usleep(100000);
             }
             // see pull request https://github.com/jmathai/php-multi-curl/pull/17
             // details here http://curl.haxx.se/libcurl/c/libcurl-errors.html
             if ($ms >= CURLM_CALL_MULTI_PERFORM) {
                 do {
                     $this->execStatus = curl_multi_exec($this->mc, $this->running);
                     usleep(intval($innerSleepInt));
                     $innerSleepInt = intval(max(1, $innerSleepInt * $this->sleepIncrement));
                 } while ($this->execStatus == CURLM_CALL_MULTI_PERFORM);
                 $innerSleepInt = 1;
             }
             $this->storeResponses();
             if (isset($this->responses[$key]['data'])) {
                 return $this->responses[$key];
             }
         }
         return null;
     }
     return false;
 }
Esempio n. 27
0
 public function send(array $messages)
 {
     if (empty($messages)) {
         return array();
     }
     $queue = curl_multi_init();
     $map = array();
     foreach ($messages as $key => $message) {
         $ch = curl_init();
         $mobiles = implode(',', $message->getMobiles());
         $u = $this->url . "?sn={$this->sn}&pwd={$this->pwd}&mobile={$mobiles}&content={$message->getContent()}&ext=&stime=&rrid=";
         curl_setopt($ch, CURLOPT_URL, $u);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_multi_add_handle($queue, $ch);
         $map[(string) $ch] = $key;
         //            $map[$key] = $ch;
     }
     $active = null;
     $responses = array();
     do {
         while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($code != CURLM_OK) {
             break;
         }
         // a request was just completed -- find out which one
         while ($done = curl_multi_info_read($queue)) {
             // get the info and content returned on the request
             $info = curl_getinfo($done['handle']);
             $error = curl_error($done['handle']);
             $results = curl_multi_getcontent($done['handle']);
             $responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');
             // remove the curl handle that just completed
             curl_multi_remove_handle($queue, $done['handle']);
             curl_close($done['handle']);
         }
         // Block for data in / output; error handling is done by curl_multi_exec
         if ($active > 0) {
             curl_multi_select($queue, 0.5);
         }
     } while ($active);
     curl_multi_close($queue);
     $errors = array();
     foreach ($responses as $key => $response) {
         if (!$response) {
             $errors[] = $messages[$key];
         } else {
             $dom = \DOMDocument::loadXML($response['results']);
             $elements = $dom->getElementsByTagName("string");
             $element = $elements->item(0);
             if (substr($element->nodeValue, 0, 1) == '-') {
                 $errors[] = $messages[$key];
             }
         }
     }
     return $errors;
 }
Esempio n. 28
0
 public function get($url_mixed, $data = array())
 {
     if (is_array($url_mixed)) {
         $curl_multi = curl_multi_init();
         $this->multi_parent = true;
         $this->curls = array();
         foreach ($url_mixed as $url) {
             $curl = new Curl();
             $curl->multi_child = true;
             $curl->beforeSend($this->before_send_function);
             $curl->success($this->success_function);
             $curl->error($this->error_function);
             $curl->complete($this->complete_function);
             $curl->base_url = $url;
             $curl->url = $this->buildURL($url, $data);
             $curl->setOpt(CURLOPT_URL, $curl->url, $curl->curl);
             $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
             $curl->setOpt(CURLOPT_HTTPGET, true);
             $this->curls[] = $curl;
             $curlm_error_code = curl_multi_add_handle($curl_multi, $curl->curl);
             if (!($curlm_error_code === CURLM_OK)) {
                 throw new \ErrorException('cURL multi add handle error: ' . curl_multi_strerror($curlm_error_code));
             }
         }
         foreach ($this->curls as $ch) {
             foreach ($this->options as $key => $value) {
                 $ch->setOpt($key, $value);
             }
         }
         do {
             curl_multi_select($curl_multi);
             $status = curl_multi_exec($curl_multi, $active);
         } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
         while (!($info_array = curl_multi_info_read($curl_multi)) === false) {
             if (!($info_array['msg'] === CURLMSG_DONE)) {
                 continue;
             }
             foreach ($this->curls as $ch) {
                 if ($ch->curl === $info_array['handle']) {
                     $ch->curl_error_code = $info_array['result'];
                     break;
                 }
             }
         }
         foreach ($this->curls as $ch) {
             $this->exec($ch);
         }
     } else {
         $this->base_url = $url_mixed;
         $this->url = $this->buildURL($url_mixed, $data);
         $this->setOpt(CURLOPT_URL, $this->url);
         $this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
         $this->setOpt(CURLOPT_HTTPGET, true);
         return $this->exec();
     }
 }
Esempio n. 29
0
 protected function curlMulti($requests)
 {
     if (!is_array($requests) || !count($requests)) {
         return false;
     }
     $chs = array();
     foreach ($requests as $request) {
         $url = isset($request['url']) ? $request['url'] : null;
         $post = isset($request['post']) ? $request['post'] : false;
         $post_data = isset($request['post_data']) && is_array($request['post_data']) ? $request['post_data'] : array();
         if ($url) {
             $ch_index = count($chs);
             $chs[$ch_index] = curl_init();
             curl_setopt($chs[$ch_index], CURLOPT_URL, $url);
             curl_setopt($chs[$ch_index], CURLOPT_RETURNTRANSFER, true);
             curl_setopt($chs[$ch_index], CURLOPT_TIMEOUT, 10);
             curl_setopt($chs[$ch_index], CURLOPT_USERAGENT, 'Teleduino PHP Class v' . $this->version);
             if ($post) {
                 curl_setopt($chs[$ch_index], CURLOPT_POST, true);
                 if (is_array($post_data) && count($post_data)) {
                     curl_setopt($chs[$ch_index], CURLOPT_POSTFIELDS, $post_data);
                 }
             }
         }
     }
     if (!count($chs)) {
         return false;
     }
     $mh = curl_multi_init();
     for ($i = 0; $i < count($chs); $i++) {
         curl_multi_add_handle($mh, $chs[$i]);
     }
     $active = null;
     do {
         $mrc = curl_multi_exec($mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($mh) != -1) {
             do {
                 $mrc = curl_multi_exec($mh, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         }
     }
     $responses = array();
     for ($i = 0; $i < count($chs); $i++) {
         $responses[$i] = curl_multi_getcontent($chs[$i]);
     }
     for ($i = 0; $i < count($chs); $i++) {
         curl_multi_remove_handle($mh, $chs[$i]);
     }
     curl_multi_close($mh);
     for ($i = 0; $i < count($chs); $i++) {
         curl_close($chs[$i]);
     }
     return $responses;
 }
 public function checkForCompletedRequests()
 {
     /*
     // Call select to see if anything is waiting for us
     if (curl_multi_select($this->multi_handle, 0.0) === -1)
         return;
     
     // Since something's waiting, give curl a chance to process it
     do {
         $mrc = curl_multi_exec($this->multi_handle, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     */
     // fix for https://bugs.php.net/bug.php?id=63411
     do {
         $mrc = curl_multi_exec($this->multi_handle, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     while ($active && $mrc == CURLM_OK) {
         if (curl_multi_select($this->multi_handle) != -1) {
             do {
                 $mrc = curl_multi_exec($this->multi_handle, $active);
             } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         } else {
             return;
         }
     }
     // Now grab the information about the completed requests
     while ($info = curl_multi_info_read($this->multi_handle)) {
         $ch = $info['handle'];
         $ch_array_key = (int) $ch;
         if (!isset($this->outstanding_requests[$ch_array_key])) {
             die("Error - handle wasn't found in requests: '{$ch}' in " . print_r($this->outstanding_requests, true));
         }
         $request = $this->outstanding_requests[$ch_array_key];
         $url = $request['url'];
         $callback = $request['callback'];
         $user_data = $request['user_data'];
         //$this->result[] = $content;
         //call_user_func($callback, $content, $url, $ch, $user_data);
         for ($i = 0; $i < 100; $i++) {
             $content = curl_multi_getcontent($ch);
             $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             $tmpRet = array();
             if ($httpCode === 200) {
                 $tmpRet = json_decode($content, true);
             }
             if (isset($tmpRet['data'])) {
                 $this->result[current($user_data)] = $tmpRet;
                 break;
             }
             usleep(15000);
         }
         unset($this->outstanding_requests[$ch_array_key]);
         curl_multi_remove_handle($this->multi_handle, $ch);
     }
 }