Faster_Image_B52f1a8_Faster_Image::batch PHP Метод

batch() публичный Метод

Get the size of each of the urls in a list
public batch ( array $urls ) : array
$urls array
Результат array
    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;
    }

Usage Example

 /**
  * Fetch images via FasterImage library
  *
  * @param array $urls_to_fetch Image src urls to fetch.
  * @param array $images Array to populate with results of image/dimension inspection.
  */
 private static function fetch_images_via_faster_image($urls_to_fetch, &$images)
 {
     if (!class_exists('Faster_Image_B52f1a8_Faster_Image')) {
         require_once AMP__DIR__ . '/includes/lib/class-faster-image-b52f1a8-faster-image.php';
     }
     $user_agent = apply_filters('amp_extract_image_dimensions_get_user_agent', self::get_default_user_agent());
     $client = new Faster_Image_B52f1a8_Faster_Image($user_agent);
     $images = $client->batch(array_column($urls_to_fetch, 'url'));
 }
Faster_Image_B52f1a8_Faster_Image