Tệp lấy nội dung từ url trong php

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à

  1. sử dụng hàm tệp ()
  2. sử dụng hàm file_get_contents()
  3. sử dụng các hàm fopen()->fread()->fclose()
  4. sử dụng cuộn tròn
  5. sử dụng chế độ ổ cắm fsockopen()
  6. sử dụng thư viện của bên thứ ba (Chẳng hạn như “snoopy”)

1. tập tin()
<?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

 

    // Initialize a file URL to the variable

    

<?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

    ____14____60

<?php1

    <?php3

    <?php5 <?php6<?php7<?php8

<?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

<?php1

     3

     5

     7

    __ 9 <?php8    1<?php8<?php5    4    5<?php8

<?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

    // Initialize a file URL to the variable0

// Initialize a file URL to the variable1// Initialize a file URL to the variable2 // Initialize a file URL to the variable3<?php0

    // Initialize a file URL to the variable6

    // Initialize a file URL to the variable8

    // Initialize a file URL to the variable0

// Initialize a file URL to the variable1// Initialize a file URL to the variable2     3<?php0

    // Initialize a file URL to the variable6

    7

đầu ra

Trước khi chạy chương trình.  

Tệp lấy nội dung từ url trong php

thư mục nguồn php

Sau khi chạy chương trình

Tệp lấy nội dung từ url trong php

Tệp đã tải xuống sau khi thực hiện thành công

Tệp lấy nội dung từ url trong php

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.   

  • Khởi tạo URL tệp cho biến
  • Tạo phiên cURL
  • Khai báo một biến và lưu tên thư mục lưu file tải về
  • Sử dụng hàm basename() để trả về tên cơ sở tệp nếu đường dẫn tệp được cung cấp dưới dạng tham số
  • Lưu tệp vào vị trí đã cho
  • Mở vị trí tệp đã lưu ở chế độ ghi chuỗi
  • Đặt tùy chọn chuyển cURL
  • Thực hiện phiên cURL và đóng phiên cURL và giải phóng tất cả tài nguyên
  • Đóng tệp

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

    9// Initialize a file URL to the variable

    9

<?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

    9

<?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<?php0

 

    9

<?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

    9

<?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

 

    9

<?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

    9

<?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

    9    1 <?php6    3<?php0

 

    9    7

    9    9

    9<?php5 <?php6<?php7<?php8

<?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

 

    9

<?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

    9<?php01 <?php6    1 <?php04<?php5<?php0

 

    9<?php09

    9<?php11 <?php6<?php13<?php8<?php01    4<?php17 0

 

    9<?php21

    9<?php23____121<?php25<?php11 0

    9<?php23____121<?php31

 

    9<?php34

    9<?php36____121 0

 

    9<?php41

    9<?php43

<?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

 

    9<?php48

    9<?php50<?php11 0

    7

đầu ra

Trước khi chạy chương trình

Tệp lấy nội dung từ url trong php

thư mục nguồn php

Sau khi chạy chương trình

Tệp lấy nội dung từ url trong php

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 .