Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl

Lựa chọn 1

Thay vì chọn dữ liệu nhị phân/thô vào một biến và sau đó viết, bạn có thể sử dụng tùy chọn

// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
2 để trực tiếp hiển thị một tệp cho Curl để tải xuống.

Đây là chức năng:

// takes URL of image and Path for the image as parameter
function download_image1[$image_url, $image_file]{
    $fp = fopen [$image_file, 'w+'];              // open file handle

    $ch = curl_init[$image_url];
    // curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; // enable if you want
    curl_setopt[$ch, CURLOPT_FILE, $fp];          // output to file
    curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, 1];
    curl_setopt[$ch, CURLOPT_TIMEOUT, 1000];      // some large value to allow curl to run for a long time
    curl_setopt[$ch, CURLOPT_USERAGENT, 'Mozilla/5.0'];
    // curl_setopt[$ch, CURLOPT_VERBOSE, true];   // Enable this line to see debug prints
    curl_exec[$ch];

    curl_close[$ch];                              // closing curl handle
    fclose[$fp];                                  // closing file handle
}

Và đây là cách bạn nên gọi nó:

// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];

Lựa chọn 2

Bây giờ, nếu bạn muốn tải xuống một tệp rất lớn, trường hợp đó trên chức năng có thể không trở nên tiện dụng. Bạn có thể sử dụng chức năng dưới đây lần này để xử lý một tệp lớn. Ngoài ra, bạn có thể in tiến độ [trong

// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
3 hoặc ở bất kỳ định dạng nào khác] nếu bạn muốn. Chức năng dưới đây được triển khai bằng cách sử dụng hàm
// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
4 ghi một đoạn dữ liệu vào tệp vào tiến trình tải xuống.If you want to download a very large file, that case above function may not become handy. You can use the below function this time for handling a big file. Also, you can print progress[in
// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
3 or in any other format] if you want. Below function is implemented using a
// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
4 function that writes a chunk of data in to the file in to the progress of downloading.

// takes URL of image and Path for the image as parameter
function download_image2[$image_url]{
    $ch = curl_init[$image_url];
    // curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, false]; // enable if you want
    curl_setopt[$ch, CURLOPT_FOLLOWLOCATION, 1];
    curl_setopt[$ch, CURLOPT_TIMEOUT, 1000];      // some large value to allow curl to run for a long time
    curl_setopt[$ch, CURLOPT_USERAGENT, 'Mozilla/5.0'];
    curl_setopt[$ch, CURLOPT_WRITEFUNCTION, "curl_callback"];
    // curl_setopt[$ch, CURLOPT_VERBOSE, true];   // Enable this line to see debug prints
    curl_exec[$ch];

    curl_close[$ch];                              // closing curl handle
}

/** callback function for curl */
function curl_callback[$ch, $bytes]{
    global $fp;
    $len = fwrite[$fp, $bytes];
    // if you want, you can use any progress printing here
    return $len;
}

Và đây là cách gọi chức năng này:

// test the download function
$image_file = "local_image2.jpg";
$fp = fopen [$image_file, 'w+'];              // open file handle
download_image2["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2"];
fclose[$fp];                                  // closing file handle

Thường thì Curl trong PHP được sử dụng để tải xuống toàn bộ trang web, một cách sử dụng tuyệt vời khác là tải xuống và lưu hình ảnh.

Sử dụng Curl để lưu hình ảnh được sử dụng trong

// test the download function
download_image1["//www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"];
5 vì bạn có thể áp dụng nhiều tham số hơn cho yêu cầu quan trọng nhất là có thể đặt tác nhân người dùng.

Với Curl, tôi có thể tuyên bố người mà tôi là người khi tôi yêu cầu hình ảnh.

Bạn có thể ẩn bot cào của mình và giả vờ bạn chỉ là một người dùng web thông thường

curl_setopt[$ch, CURLOPT_USERAGENT, "Mozilla/5.0 [Windows; U; Windows NT 5.1; en-US]"];

Hoặc có một số niềm vui:

curl_setopt[$ch, CURLOPT_USERAGENT, "Grabbing image in 3, 2, 1... Thanks"];

Rõ ràng một người dùng thường xuyên sẽ không đạt được nhiều yêu cầu trong thời gian ngắn như một bot cào.

Bằng cách sử dụng

curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1];
curl_setopt[$ch, CURLOPT_BINARYTRANSFER, 1];

Hình ảnh được trả về dưới dạng chuỗi nhị phân thô và không xuất ra.

curl_setopt[$ch, CURLOPT_HEADER, false];

Có nghĩa là không có tiêu đề nào ở đầu ra.

$fp = fopen[$save_as, 'x'];
fwrite[$fp, $raw_data];
fclose[$fp];

Xử lý việc lưu dữ liệu hình ảnh thô vào tệp hình ảnh có thể xem thực tế.

Mã đầy đủ là:

Bài Viết Liên Quan

Chủ Đề