PHP Getting Document of Remote Address

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

Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL

Read more articles

General Knowledge

Learn Popular Language