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("http://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("http://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("http://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("http://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("http://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("http://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("http://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("http://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à:

Tôi cần lưu một hình ảnh từ URL bằng Curl và lưu nó vào thư mục trên máy chủ của tôi. Tôi đã chiến đấu với mã này nhưng không có kết quả. Lý tưởng nhất là tôi muốn lấy hình ảnh và lưu nó dưới dạng "Photo1" hoặc một cái gì đó. Cứu giúp!

// test the download function
download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg");
0

Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
Ngày 14 tháng 11 năm 2020in Phpby • & NBSP; 37,510 điểm • 3.756 lượt xem in PHP by
• 37,510 points
3,756 views

1 Câu trả lời cho câu hỏi này.

Xin chào @Kartik,

thử cái này:

// test the download function
download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg");
1

và đảm bảo rằng trong php.ini cho phép_url_fopen có thể bật

Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
Đã trả lời ngày 14 tháng 11 năm 2020by Niroj • & NBSP; 82.800 điểm Nov 14, 2020 by Niroj
• 82,800 points

Các câu hỏi liên quan trong PHP

  • Tất cả danh mục
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Apache Kafka (84)(84)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Apache Spark (596)(596)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Azure (131)(131)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Dữ liệu lớn Hadoop (1.907)(1,907)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Blockchain (1.673)(1,673)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    C# (135)(135)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    C ++ (270)(270)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Tư vấn nghề nghiệp (1.060)(1,060)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Điện toán đám mây (3,391)(3,391)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    An ninh mạng & hack đạo đức (147)(147)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Phân tích dữ liệu (1.266)(1,266)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Cơ sở dữ liệu (855)(855)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Khoa học dữ liệu (75)(75)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    DevOps & Agile (3.500)(3,500)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Tiếp thị kỹ thuật số (111)(111)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Các chủ đề về sự kiện & xu hướng (28)(28)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    IoT (Internet of Things) (387)(387)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Java (1.225)(1,225)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Kotlin (3)(3)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Quản trị Linux (384)(384)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Học máy (337)(337)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    MicroStrargety (6)(6)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    PMP (423)(423)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Power BI (516)(516)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Python (3.161)(3,161)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    RPA (650)(650)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Salesforce (92)(92)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Selenium (1.569)(1,569)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Kiểm tra phần mềm (56)(56)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Tableau (608)(608)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Talend (73)(73)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Các loại (124)(124)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Phát triển web (3,002)(3,002)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Hỏi chúng tôi bất cứ điều gì! (66)(66)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Những người khác (1.466)(1,466)
  • Hướng dẫn download image from url php curl - tải hình ảnh từ url php curl
    Phát triển di động (180)(180)

Đăng ký nhận bản tin của chúng tôi và nhận được các khuyến nghị cá nhân hóa.

Bạn co săn san để tạo một tai khoản? Đăng nhập.