RETRIEVE A REMOTE RESOURCE WITH fopen()
Sometimes you may need to connect to a REST service to retrieve some data, or simply download a file through a standard HTTP or FTP connection.
The PHP fopen() function is incredibly helpful because it doesn’t just work for local files, but you can also use it to read data from remote locations using different protocols.
In fact, fopen() supports a large number of wrappers, or protocol handlers, that let it connect to local or remote resource through different communication protocols always using the same syntax.
You can use fopen() to get a webpage content and edit it, a common requirement in work environments.
In the following example we change the Google homepage’s title:
If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.
If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!
MySQLi vs PDO: which one is best for you?
Get my weekly PHP tips and the bonus PDF, absolutely free.