Last active
December 24, 2018 14:18
-
-
Save duskohu/d7829a046d6732abab31200447be12da to your computer and use it in GitHub Desktop.
curl-multy
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
<?php | |
$urls = array( | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59914d4420a17/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991501203db9/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991508463df6/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599150f30d32a/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-598ec4850dd48/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-598df691c7e53/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-598df6511a99e/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-597f621b613cf/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991a9a70b0bf/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991a9dade0a4/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991aa0c13b0e/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991aa394ea02/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991aa65233c0/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991ab180bd75/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991ab314ff93/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991ab4fa6c21/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991ab7db80b3/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991abb08e5ac/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991abd126f72/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991abf73d71c/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991f29be3833/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5991f2cf7057e/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5992b1a20deec/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5992b1d901ecf/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5992b1f597fbe/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5992b22551817/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599349b30a2d5/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599349d6549b4/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599349f64adcd/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936d9c6730a/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936dbd2e2c9/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936df3ab72e/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936e206bca0/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936e47ddf20/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936e6cf08b6/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936eaba0077/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936ed37bedb/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936efab8a34/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936f20be309/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59936f54d1e17/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599404bff3f1d/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599404eb238cb/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599405153e62f/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-599405436deba/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5994056da5f82/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-5994059959022/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59945baee5725/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59945bec61e76/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59945c26d9b02/', | |
'https://affil.invia.sk/direct/core/tool_xml-feed/download/id/4717246-59945c5513fe7/', | |
); | |
$tempDir = 'files-d'; | |
if (!file_exists($tempDir)) { | |
mkdir($tempDir, 0777); | |
} | |
$mh = curl_multi_init(); | |
$ch = array(); | |
foreach ($urls as $key => $value) { | |
$ch[$key] = curl_init($value); | |
curl_setopt($ch[$key], CURLOPT_HEADER, FALSE); | |
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, TRUE); | |
curl_multi_add_handle($mh, $ch[$key]); | |
} | |
do { | |
curl_multi_exec($mh, $running); | |
curl_multi_select($mh); | |
} while ($running > 0); | |
$exception = FALSE; | |
$info = array(); | |
foreach ($urls as $key => $value) { | |
$curlError = curl_error($ch[$key]); | |
$httpCode = curl_getinfo($ch[$key], CURLINFO_HTTP_CODE); | |
$effectiveUrl = curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL); | |
$info[] = curl_getinfo($ch[$key]); | |
if ($curlError) { | |
$exception = $effectiveUrl . ', Item:' . $key . ', Error: ' . $curlError; | |
curl_multi_remove_handle($mh, $ch[$key]); | |
break; | |
} elseif ($httpCode != 200) { | |
$exception = $effectiveUrl . ', Item:' . $key . ', Status: ' . $httpCode; | |
curl_multi_remove_handle($mh, $ch[$key]); | |
break; | |
} else { | |
$pathToFile = $tempDir . '/' . $key . '.xml'; | |
$content = curl_multi_getcontent($ch[$key]); | |
file_put_contents($pathToFile, $content); | |
// process file | |
//unlink($pathToFile); | |
curl_multi_remove_handle($mh, $ch[$key]); | |
} | |
} | |
curl_multi_close($mh); | |
if ($exception) { | |
throw new \Exception($exception); | |
} | |
echo '<pre>'; | |
var_dump($info); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment