If you want to use PHP Programming to include web content from another website, you have a great set of tools to do so. There are a number of ways to include remote web code. Your choice is based on the type of code being included, whether the included code needs to be modified, and how the remote code is to be used.
The primary PHP Programming methods available are file_get_contents and curl. There are other ways to retrieve remote code, but these two handle the vast majority of the programmer’s PHP Programming needs.
file_get_contents acts similar to your we browser and makes a simple web request to the target server and web file. It is capable of other protocols such as FTP, but we’ll deal with HTTP here. If the file_get_contents attempt fails, a “fail” is returned. This results when the network connection is unavailable, the target server is down, the file doesn’t exist, or some other similar problem prevents accessing the requested data.
file_get_contents can crop the data by defining a start and stop point, which are character point values in the block of code. Let’s say your target is a web page with 1000 characters of PHP Code. The actual target is a subsection of code starting at character 250 and ending at character 500. file_get_contents will extract that subsection of code only. Of course, it’s best if you know the content doesn’t change, else your results may vary when the code changes.
file_get_contents requires that you encode your URIs, so space characters must be denoted as %20 and so on. The configurations on this command can be left empty, and simply target the URI with default values. Be careful when using Windows and Microsoft IIS when the target is the HTTPS protocol, as IIS doesn’t handle the method perfectly.
If you are going to simply display the results (retrieved code) from file_get_contents, you can simply echo the results into your page. If the results of file_get_contents require modification before bing used or displayed, the results should be stored into a variable for subsequent modification. You can modify the results without dumping the results into a variable, and is a personal choice. Once you have stored the web code into a variable, you can run eregi_replace, str_replace, and other PHP String Commands on it. You may want to replace global addresses with local ones, or perhaps convert local pathways to global. Style Code can be replaced with your own local Style Code.
CAVEATS: It’s obviously frowned upon if you represent someone else’s code as your own. Review your target web page carefully and determine if it will change, how it might affect your own presentation, and decide if you trust the target domain. Beware using PHP Code such as the include() command in place of file_get_contents since that can spell disaster. The include command can load remote PHP Code that will execute on your own server, possibly compromising your web site, your databases, or even your entire web server.