Fetching a Web Page From Your PHP Code
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:
<?php function fetch_page($url) { /* get hostname and path */ $host = parse_url($url, PHP_URL_HOST); $path = parse_url($url, PHP_URL_PATH); if (empty($path)) { $path = "/"; } /* Build HTTP 1.0 request header. Defined in RFC 1945 */ $headers = "GET $path HTTP/1.0\r\n" . "User-Agent: myHttpTool/1.0\r\n\r\n"; /* open socket connection to remote host on port 80 */ $fp = fsockopen($host, 80, $errno, $errmsg, 30); if (!$fp) { /* ...some error handling... */ return false; } /* send request headers */ fwrite($fp, $headers); /* read response */ while(!feof($fp)) { $resp .= fgets($fp, 4096); } fclose($fp); /* separate header and body */ $neck = strpos($resp, "\r\n\r\n"); $head = substr($resp, 0, $neck); $body = substr($resp, $neck+4); /* omit parsing response headers */ /* return page contents */ return($body); } ?>
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:
- No dependencies
- Support for HTTP 1.1
- Support redirects
- Support proxies
- Support HEAD and GET methods. This might be useful for link checker applications
Keywords: web fetching, fetch web page, fetch web from php, php http client
Share:
Save to del.icio.us
Digg this!



Add your comment