static function getInstance() { if (self::$instance == null) { self::$instance = new MultiCurl(); } return self::$instance; }
private static function MultiCurlExec($requestList) { MultiCurl::instance()->open(); MultiCurl::instance()->send($requestList); $responseList = MultiCurl::instance()->exec(); MultiCurl::instance()->close(); return $responseList; }
public function curl_multi_add_handle() { // 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); $running = null; //execute the handles do { curl_multi_exec($mh->getMultiHandle(), $running); } while ($running > 0); //close all the handles unset($mh); }
private function flatten($html) { $multi = MultiCurl::getInstance(); $hdls = array(); foreach (array('js', 'css') as $type) { $hdls[$type] = array(); ${$type} = ''; } /* scripts */ preg_match_all("/<script[^<>]+src=['\"]([^<>'\"]+)['\"][^<>]*>(.*)<\\/script>/i", $html, $scripts); $html = str_replace($scripts[0], '', $html); foreach ($scripts[1] as $url) { $hdl = $this->getCurlHandleForUrl($url); $multi->add($hdl); $hdls['js'][] = (string) $hdl; } /* stylesheets */ preg_match_all("/<link[^<>]+href=['\"]([^<>'\"]+.css)['\"][^<>]*[\\/]*>((<\\/link>)*)/i", $html, $links); // This could do better by checking for the rel attribute... $html = str_replace($links[0], '', $html); foreach ($links[1] as $url) { $hdl = $this->getCurlHandleForUrl($url); $multi->add($hdl); $hdls['css'][] = (string) $hdl; } /* append results */ foreach ($hdls as $key => $ids) { foreach ($ids as $id) { $res = $multi->get($id); if ($res) { ${$key} .= $res; } } } if ($js) { $html = '<script>' . $js . '</script>' . $html; } if ($css) { $html = '<style>' . $css . '</style>' . $html; } return $html; }
/** * 添加到MultiCurl中去 * @param MultiCurl $multiCurl * @return int */ public function addToMultiCurl(MultiCurl $multiCurl) { $this->multiCurlAddedTo = $multiCurl; return curl_multi_add_handle($multiCurl->getHandle(), $this->getHandle()); }
public function actionTestMultiCurlInstantGetAndNewOne() { $mc = new MultiCurl(); for ($i = 0; $i < 10; $i++) { $mc->addUrl("http://test/testMultiCurl/longTask", array('interval' => 300, 'times' => 30, 'name' => 'Jack' . $i), 'GET'); } $mc->exec(); $mc->wait(function ($singleCurl) use($i, $mc) { echo "URL: " . $singleCurl->getUrl() . ' <br>'; echo "CONTENT: " . $singleCurl->getResult() . ' <br>'; for (; $i < 20; $i++) { $mc->addUrl("http://test/testMultiCurl/longTask", array('interval' => 300, 'times' => 30, 'name' => 'Tom-' . $i), 'GET'); } }); echo "<hr> after all: <br/>"; $results = $mc->getResults(); foreach ($results as $result) { echo "URL: " . $result->getUrl() . ' <br>'; echo "CONTENT: " . $result->getResult() . ' <br>'; } }
function getContentFromWebPage($productUrls) { $options = array(CURLOPT_USERAGENT => $this->setUserAgent(), CURLOPT_HTTPHEADER => $this->setHeader(), CURLOPT_FOLLOWLOCATION => true, CURLOPT_REFERER => $this->merchantUrl); return MultiCurl::request($productUrls, $options); }
function _pugpig_package_download_batch($heading, $entries, $debug, $concurrent) { print_r("<h3>Downloading " . $heading . " - " . count($entries) . " files</h3>"); if ($debug) { var_dump($entries); } // Check the URLs we're about to download are real URLs $format_failures = array(); foreach (array_keys($entries) as $entry) { if (!filter_var($entry, FILTER_VALIDATE_URL)) { $format_failures[$entry] = "Invalid URL format: " . $entry; unset($entries[$entry]); } else { $path = $entries[$entry]; if (strpos($path, "?") || strpos($path, "&")) { $format_failures[$entry] = "Invalid local disk path: " . $path; unset($entries[$entry]); } } } _pugpig_package_show_failures($format_failures); // Both of these should become a settings, as well as timeout value $warning_file_size = 150 * 1024; // 150 Kb $mc = new MultiCurl($entries, $concurrent, $warning_file_size); $mc->process(); $failures = $mc->getFailures(); if (count($failures) > 0) { _pugpig_package_show_failures($failures); _print_immediately('<b>Aborting</b><br /><a href="javascript:location.reload(true);">Refresh this page to reload and try again. (It will resume from where it last succeeded.)</a><br />'); exit; } else { print_r("<em>Done</em><br />"); } return $entries; }
public function __construct($key) { $this->key = $key; $this->epiCurl = MultiCurl::getInstance(); }
function _pugpig_package_download_batch($heading, $entries, $debug = false, $concurrent = 3) { print_r("<h3>Downloading " . $heading . " - " . count($entries) . " files</h3>"); // Check the URLs we're about to download are real URLs $format_failures = array(); foreach (array_keys($entries) as $entry) { if (!_pugpig_package_validate_download_url($entry, $entries[$entry], $format_failures)) { unset($entries[$entry]); } } _pugpig_package_show_failures($format_failures); // Both of these should become a settings, as well as timeout value $warning_file_size = 150 * 1024; // 150 Kb $mc = new MultiCurl($entries, $concurrent, $warning_file_size); $mc->process(); $successes = $mc->getSuccesses(); $failures = $mc->getFailures(); _pugpig_show_batch($successes); // _pugpig_array_to_table($successes); if (count($failures) > 0) { _pugpig_package_show_failures($failures); _print_immediately('<b>Aborting</b><br /><a href="javascript:location.reload(true);">Refresh this page to reload and try again. (It will resume from where it last succeeded.)</a><br />'); exit; } else { print_r("<em>Done</em><br />"); } return $entries; }
public function testSimple() { $this->markTestSkipped('Looks bad'); $multi = new MultiCurl(); $multi->setCompleteCallback(function ($curl) { $info = $curl->getInfo(); # printf("%.2F: %s\n", $info['total_time'], $info['url']); }); $curl = new Curl(); $curl->setUrl('http://www.golem.de/'); $response1 = $curl->getResponse(); $curl->execute(); $multi->addCurl($curl); $curl = new Curl(); $curl->setUrl('http://www.heise.de/'); $response2 = $curl->getResponse(); $multi->addCurl($curl); $curl = new Curl(); $curl->setUrl('http://www.example.com/'); $response3 = $curl->getResponse(); $multi->addCurl($curl); $curl = new Curl(); $curl->setUrl('http://localhost/'); $response4 = $curl->getResponse(); $multi->addCurl($curl); $curl = new Curl(); $curl->setUrl('dasdasdasfdf432rwedfs://localhost/'); $response5 = $curl->getResponse(); $multi->addCurl($curl); $index = 0; while ($multi->active) { $index++; $multi->step(); if ($index == 1) { $curl = new Curl(); $curl->setUrl('http://www.example.com/1'); $multi->addCurl($curl); } if ($index == 1) { $curl = new Curl(); $curl->setUrl('http://www.example.com/2'); $multi->addCurl($curl); } if ($index == 3) { $curl = new Curl(); $curl->setUrl('http://www.example.com/3'); $multi->addCurl($curl); } } }