How to Retrieve and Get the Contents of a Web Page Using PHP 


               


How to Retrieve and Get the Contents of a Web Page Using PHP



PHP







In this article, we show ways of how to retrieve a web page using PHP to get the contents of the web page.

Being that PHP is a dynamic, server-side language, it can access other remote pages on the web at your request.

It can fetch any URL so that you can get the contents of the page of that URL.

This can be done in a few ways.

file_get_contents() function

The first way we can retrieve and get the contents of a page is through the file_get_contents() function.

This takes the contents of the page specified in the parameter and gets the contents of it on the current page.

The PHP code to do this is shown below.



So this PHP code above retrieves the ebay.com home page and puts the full contents on the page. So it's almost as if your site has ebay's full page.

This function is only supposed to do used to get the contents of a page so that you can see what's on the page and maybe scrape some of the information on the page, such as web scraping. Even though it appears like your site has actually transformed into ebay, it hasn't. You simply have the contents of ebay's home page on your site, not the functionality. If you try to use the search text box to look for an item on ebay, it won't work. Your site hasn't become ebay. It's simply loaded the contents of ebay's page onto your website. The file_get_content(), as the name implies, is simply to get the contents of a web page, mostly the text. The actual images, themselves will be missing the majority of the time, because that information is in the folder of any website, not on yours. However, you can still get the contents of an image, such as the image source and the alt tags, if you scrape for it.

If you simply want to redirect to another site, you would use the PHP Location header redirect function.

CURL

Another way of retrieving and getting the contents of a page is through the CURL function.

This achieves the same effect as above, with the file_get_contents() function.

The PHP code to do this with CURL is shown below.



This code achieves the same effect.

It retrieves the URL inserted into it and gets the contents of that page.

So these are 2 ways we can retrieve and get the contents of a URL using PHP.


Related Resources

How to Insert Images into a MySQL Database Using PHP

How to Insert Files into a MySQL Database Using PHP

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Redirect to Another URL with PHP

How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Search a MySQL Table For Any Word or Phrase Using PHP

How to Create a Send Email Form Using PHP and MySQL

How to Create a Newsletter from Scratch using PHP and MySQL

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

HTML Comment Box is loading comments...