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()

<?php

$url=’https://oscarliang.com’;

// using file() function to get content

$lines_string=implode(”,$lines_array);

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.

<?php

$url=’https://oscarliang.com’;

//file_get_contents() reads remote webpage content

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

?>

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

<?php

$url=’https://oscarliang.com’;

//fopen opens webpage in Binary

// read content line by line

// initialize

$lines_string=””;

do{

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

$data=fread($handle,1024);

$lines_string.=$data;

fclose($handle);

}while(true);

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

<?php

$url=’https://oscarliang.com’;

$ch=curl_init();

$timeout=5;

curl_setopt($ch, CURLOPT_URL, $url);

// Get URL content

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$lines_string=curl_exec($ch);

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

// close handle to release resources

echo $lines_string;

?>

۵٫ fsockopen()函数 socket模式

<?php

$fp = fsockopen(“t.qq.com”, 80, $errno, $errstr, 30);

if (!$fp) {

echo “$errstr ($errno)

} else {

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

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

$out .= “Connection: Closernrn”;

fclose($fp);

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.

<?php

// include snoopy library

require(‘Snoopy.class.php’);

$url = “http://t.qq.com”;

$snoopy = new Snoopy;

// read webpage content

$lines_string = $snoopy->results;

$snoopy->fetch($url);

// save it to $lines_string

echo $lines_string;

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

?>

?>

5 سال پیش

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

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

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