In some cases it is necessary to insert an HTML code into pages that is regularly generated by another server. The method using inserting this code into
IFRAME is commonly used, but this method brings a number of problems (from the impossibility of styling the content with ease up to SEO optimizations). Visual PHP™ contains the
download() function that downloads URL content from a foreign server and returns the content into a variable. The content of this variable can be displayed directly in the content of the pages in a usual way.
Properties of the Download Function
The
download function is defined as follows:
function download($url, $post=array(), $cookies=array(), $params=array())
The following parameters can be given to this function:
| $url |
An URL address to be downloaded. (This may be an HTML page or a file link.) |
| $post |
A field with parameters that is send using the POST method. (It is used to simulate downloading the content generated by forms.) |
| $cookies |
A field with the parameters of the content of cookies. (Some servers verify the content of cookies, e.g. for logging in.) |
| $params |
A field with parameters extending the settings of this function.
| timeout |
The number of second upon the lapse which the function aborts downloading. Default value: 20 s |
| followRedirect |
In the case of redirection by means of HTTP REDIRECT this redirection follows. Default value: true |
| headersOnly |
It will return HTTP headers only. (This allows e.g. getting the size of a file before downloading it or determining whether a page exists.) Default value: false |
| headersAsArray |
If the headersOnly option is on, it will return HTTP headers parsed as an array. Default value: false |
| agent |
User Agent – A header in which browsers send their name, version and platform or browser robots are identified by means of it. You can identify a server this way, e.g. as FireFox 3 under Windows or googlebot. |
|
The aforementioned parameters
$post,
$cookies and
$params can be used on such servers only on which a CURL extension is available. If it be to the contrary, you can use the
$url parameter only (and load using the GET method only).
Displaying Downloaded Content on Your Own Pages
At the beginning, it is necessary to understand that it is not possible to insert a complete downloaded page into pages. All pages contain the headers
<head> and the body
<body>. If we enter this code as a whole into the pages, the page would break completely and lose its validity. Consequently, it is necessary for an URL from which we download the data to contain the HTML of the content only and not the complete HTML page. That is why it is necessary to have such clean content prepared by the operator of external pages. This has the advantage that such contain displayed will respect CSS styles defined on your local pages.
An example of a script for downloading and displaying content
$content = download("http://www.domena.cz/export/obsah.html");
echo $content;
Sharing Content on Your Own Project under Visual PHP™
For the case you want certain parts of a Web site to be shared by your projects under Visual PHP™, we will show you how to prepare such content. It is necessary to create such script on a remote server from which we want to load data that will generate the content. As an example, we will give the script that will return the HTML of the content of a list of the last 5 news. Go to the
Presentation -> Scripts menu, create a new script with
tid, e.g.
news_export, and insert the following source code into it:
$content = getTable("news")->getContent("itemsPerPage=5,orderBy=date DESC,template=news_export");
echo $content;
exit;
The
getContent method of a table object functions completely in the same way as a
Table Content component – consequently, it will return the content of a table processed by a template. We will display this content in the script and use the
exit command to terminate the processing. Thanks to this termination, neither the content of the global layout nor the
<head> headers nor other tags but the clean HTML of the content of the list of news will be displayed.
In the project that is to load and display this content, you have only to enter the following PHP code to the place where it is required:
$content = download("http://www.domena.cz/scripts.php?tid=news_export");
echo $content;