Last active
April 24, 2020 21:50
-
-
Save luxumbra/51aa554cd163d96f99fad9837992a426 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
<?php | |
static function get_screenshots( | |
$sites, | |
$endpoint, | |
$token, | |
$device_width, | |
$device_height | |
) { | |
//An array that will contain all of the information | |
//relating to each request. | |
$requests = array(); | |
$query = ""; | |
//Initiate a multiple cURL handle | |
$mh = curl_multi_init(); | |
//Loop through each URL. | |
foreach ($sites['sites'] as $k => $site) { | |
// var_dump($k); | |
$url = $site["uri"]; | |
$query .= "?url=$url&apikey=$token&width=$device_width&height=$device_height"; | |
$requests[$k] = array(); | |
$requests[$k]['url'] = $endpoint . $query; | |
$requests[$k]['site'] = $url; | |
//Create a normal cURL handle for this particular request. | |
$requests[$k]['curl_handle'] = curl_init($url); | |
//Configure the options for this request. | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_TIMEOUT, 10); | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_CONNECTTIMEOUT, 10); | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($requests[$k]['curl_handle'], CURLOPT_SSL_VERIFYPEER, false); | |
// var_dump($requests[$k]); | |
//Add our normal / single cURL handle to the cURL multi handle. | |
curl_multi_add_handle($mh, $requests[$k]['curl_handle']); | |
} | |
//Execute our requests using curl_multi_exec. | |
$stillRunning = false; | |
do { | |
curl_multi_exec($mh, $stillRunning); | |
} while ($stillRunning); | |
//Loop through the requests that we executed. | |
foreach ($requests as $k => $request) { | |
print_r($request); | |
//Remove the handle from the multi handle. | |
curl_multi_remove_handle($mh, $request['curl_handle']); | |
//Get the response content and the HTTP status code. | |
$requests[$k]['content'] = curl_multi_getcontent($request['curl_handle']); | |
$requests[$k]['http_code'] = curl_getinfo($request['curl_handle'], CURLINFO_HTTP_CODE); | |
//Close the handle. | |
curl_close($requests[$k]['curl_handle']); | |
} | |
//Close the multi handle. | |
curl_multi_close($mh); | |
//var_dump the $requests array for example purposes. | |
// var_dump($requests); | |
foreach ($requests as $j => $response) { | |
// pass the site name and screenshot file to save func for processing and saving to the server. | |
self::save_screenshot($response[$j]['site'], $response[$j]['content']); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment