To fetch a web page from your PHP application, you could use curl functions or simply open the page with fopen(). But these have some limitations. Your server needs to have PHP with curl enabled, or the PHP configuration should allow scripts to open URLs with fopen().
If that is not the case, you can still fetch a web page by opening a socket connection to the remote host and make HTTP request. It looks something like this:
Listing 1: fetch_page.php
With the function above you should be able to fetch a page without any dependencies. However, it lacks many important features. You may want to add some looping for page redirects and add support for HTTP 1.1. For a more complex HTTP client, see my EasyWebFetch class.
EasyWebFetch is a simple class for web fetching from your application. This class is an alternative if your server doesn't have PHP with curl enabled, or the PHP configuration doesn't allow opening URLs with fopen(). This class fetch a web page by opening socket connection to remote host, so it has no dependencies and should work on any server configuration.
Features:
Binny V A on Oct 23, 2008:
blaaze on Dec 14, 2008:
Taree on Jul 5, 2009:
Nash on Jul 5, 2009:
Avijit on Feb 2, 2010:
article volcano on Feb 7, 2010:
Chris on May 7, 2010:
function fetch_page($url) {
/* get hostname and path */
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$query = parse_url($url, PHP_URL_QUERY);
if (empty($path)) {
$path = "/";
}
if (!empty($query)) {
$path = $path."?$query";
}
Anonymous on Oct 27, 2010:
Nash on Oct 28, 2010:
| ICQ | 489571630 |
| Skype | dede_bl4ckheart |
| Yahoo | dede_bl4ckheart |
| nashruddin.amin |
Abbas Khan on Aug 27, 2008:
Looking into curl screwed my head ! LOL