php Getting webpage content

There are so far 6 ways of Getting webpage content (full HTML) in PHP are most commonly used. The methods are

  1. using file() fuction
  2. using file_get_contents() function
  3. using fopen()->fread()->fclose() functions
  4. using curl
  5. using fsockopen() socket mode
  6. using Third party library (Such as “snoopy”)

۱٫ file()



// using file() function to get content


echo $lines_string;


۲٫ file_get_contents()

To use file_get_contents and fopen you must ensure “allow_url_fopen” is enabled. Check php.ini file, turn allow_url_fopen = On. When allow_url_fopen is not on, fopen and file_get_contents will not work.



//file_get_contents() reads remote webpage content

//output, you can also save it locally on the server


۳٫ fopen()->fread()->fclose()



//fopen opens webpage in Binary

// read content line by line

// initialize



if(strlen($data)==0) {





//close handle to release resources


echo $lines_string;

۴٫ curl

You need to have curl enabled to use it. Here is how: edit php.ini file, uncomment this line: extension=php_curl.dll, and install curl package in Linux





curl_setopt($ch, CURLOPT_URL, $url);

// Get URL content

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);


//output, you can also save it locally on the server

// close handle to release resources

echo $lines_string;


۵٫ fsockopen()函数 socket模式


$fp = fsockopen(“”, 80, $errno, $errstr, 30);

if (!$fp) {

echo “$errstr ($errno)

} else {

$out .= “Host: t.qq.comrn”;

$out = “GET / HTTP/1.1rn”;

$out .= “Connection: Closernrn”;


fwrite($fp, $out);

while (!feof($fp)) {

echo fgets($fp, 128);



۶٫ snoopy library

This library has recently become quite popular. It’s very simple to use. It simulates a web browser from your server.


// include snoopy library


$url = “”;

$snoopy = new Snoopy;

// read webpage content

$lines_string = $snoopy->results;


// save it to $lines_string

echo $lines_string;

//output, you can also save it locally on the server



5 سال پیش

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.