PHP Getting Document of Remote Address

In this article, you will learn how to get a document from a Remote Address using the PHP language. For this, first we request data from a remote server and then open this in our script. We will use the PHP function fopen() to connect to the web address of the desired document and open the requesting page in the browser.

fopen() function

This function opens a file or URL. It returns a file pointer resource on success, or false on error.

Syntax of fopen()

fopen(filename, mode, path, context)

filename - It specifies the filename or url to open.
mode - It specifies the mode to access the file, like - r (read), w (write), r+ (read and write).
path - It is an optional parameter which is set to 1 if you want to search for the file in the include_path.
context - It is an optional parameter which specifies the context of the file handle.

The below example opens the document of the given url using fopen() function in reading mode.

	<title>Getting Document of Remote Address</title>
		$webpage = "";
		$fp = fopen($webpage, "r") or die("Error");
		while(!feof($fp)) {
			print fgets($fp, 1024);

When you execute the above code, you will get the web page of the given url in your browser as shown below.

Getting Document of Remote Address

Notice that, when you open the document to the browser, the images on the page may be broken because the paths to the images in IMG elements are often relative.

Related Articles

PHP User Authentication by IP Address
How to encrypt password in PHP
Different datatype comparison in PHP
PHP loop through an associative array
PHP CURL Cookie Jar
PHP remove last character from string
PHP calculate percentage of total
Insert image in database using PHP
PHP set a cookie to store login detail
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
PHP pagination
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
QR code generator PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL

Read more articles

General Knowledge

Learn Popular Language