Cho đến nay có 6 cách Lấy nội dung trang web (HTML đầy đủ) bằng PHP được sử dụng phổ biến nhất. Các phương pháp là Show
<?php $url='https://oscarliang.com'; // using file() function to get content $lines_array=file($url); // turn array into one variable $lines_string=implode('',$lines_array); //output, you can also save it locally on the server echo $lines_string; ?>2. file_get_contents() Để sử dụng file_get_contents và fopen, bạn phải đảm bảo bật “allow_url_fopen”. kiểm tra php. ini, bật allow_url_fopen = Bật. Khi allow_url_fopen không bật, fopen và file_get_contents sẽ không hoạt động <?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>3. fopen()->fread()->fclose() <?php $url='https://oscarliang.com'; //fopen opens webpage in Binary $handle=fopen($url,"rb"); // initialize $lines_string=""; // read content line by line do{ $data=fread($handle,1024); if(strlen($data)==0) { break; } $lines_string.=$data; }while(true); //close handle to release resources fclose($handle); //output, you can also save it locally on the server echo $lines_string; ?>4. Xoăn Bạn cần kích hoạt curl để sử dụng nó. Đây là cách: chỉnh sửa php. ini, bỏ ghi chú dòng này. tiện ích mở rộng = php_curl. dll và cài đặt gói curl trong Linux <?php $url='https://oscarliang.com'; $ch=curl_init(); $timeout=5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // Get URL content $lines_string=curl_exec($ch); // close handle to release resources curl_close($ch); //output, you can also save it locally on the server echo $lines_string; ?>5. fsockopen()函数 socket模式 <?php $fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno) n"; } else { $out = "GET / HTTP/1.1rn"; $out .= "Host: t.qq.comrn"; $out .= "Connection: Closernrn"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>6. thư viện rình mò Thư viện này gần đây đã trở nên khá phổ biến. Nó rất đơn giản để sử dụng. Nó mô phỏng một trình duyệt web từ máy chủ của bạn Trong bài viết này, chúng ta sẽ xem cách tải xuống và lưu tệp từ URL trong PHP, đồng thời cũng sẽ hiểu các cách khác nhau để thực hiện nó thông qua các ví dụ. Có nhiều cách tiếp cận để tải xuống một tệp từ một URL, một số trong số chúng sẽ được thảo luận bên dưới Sử dụng hàm file_get_contents(). Hàm file_get_contents() được sử dụng để đọc một tệp thành một chuỗi. Chức năng này sử dụng các kỹ thuật ánh xạ bộ nhớ được máy chủ hỗ trợ và do đó nâng cao hiệu suất, khiến nó trở thành một cách ưa thích để đọc nội dung của tệp cú pháp file_get_contents($path, $include_path, $context, $start, $max_length) ví dụ 1. Ví dụ này minh họa việc sử dụng hàm file_get_contents() để đọc tệp thành một chuỗi PHP
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>1 <?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>2
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>1 0
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>1 8
đầu ra Trước khi chạy chương trình. thư mục nguồn php Sau khi chạy chương trình Tệp đã tải xuống sau khi thực hiện thành công Tệp hình ảnh đã tải xuống Sử dụng PHP Curl. cURL là viết tắt của 'Ứng dụng khách cho URL', ban đầu với URL được đánh vần bằng chữ hoa để làm rõ rằng nó xử lý các URL. Nó được phát âm là 'xem URL'. Dự án cURL có hai sản phẩm libcurl và curl Các bước tải file.
Ví dụ. Ví dụ này minh họa việc sử dụng PHP Curl để thực hiện các yêu cầu HTTP trong PHP, để tải xuống tệp PHP
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>1 <?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>2
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>4 <?php 0
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>19
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>21 <?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>22______11 0
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>27
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>29
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>1 0
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>49
<?php $url='https://oscarliang.com'; //file_get_contents() reads remote webpage content $lines_string=file_get_contents($url); //output, you can also save it locally on the server echo htmlspecialchars($lines_string); ?>21 0
đầu ra Trước khi chạy chương trình thư mục nguồn php Sau khi chạy chương trình Tệp hình ảnh đã tải xuống PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này Làm cách nào để đọc nội dung của một URL trong PHP?php echo readfile("http. //ví dụ. com/"); //cần bật "Allow_url_include" //HOẶC echo include("http. //ví dụ. com/"); //cần bật "Allow_url_include" //HOẶC echo file_get_contents("http. //ví dụ. com/"); //HOẶC echo stream_get_contents(fopen('http. //ví dụ. com/', "rb"));
Làm cách nào để lấy dữ liệu từ URL trong PHP?Các tham số từ một chuỗi URL có thể được truy xuất trong PHP bằng cách sử dụng các hàm parse_url() và parse_str() .
Làm cách nào để lấy nội dung của tệp trong PHP?The file_get_contents() đọc một tệp thành một chuỗi. Hàm này là cách ưa thích để đọc nội dung của tệp thành một chuỗi. Nó sẽ sử dụng các kỹ thuật ánh xạ bộ nhớ, nếu điều này được máy chủ hỗ trợ, để nâng cao hiệu suất.
Làm cách nào để lấy dữ liệu từ tệp văn bản trong PHP?Hàm fgetc() của PHP dùng để đọc ký tự đơn từ tệp. Để lấy tất cả dữ liệu bằng hàm fgetc(), hãy sử dụng . hàm feof() bên trong vòng lặp while . |