PHP, Tip & Tricks - Written by admin on Tuesday, May 6, 2008 23:28 - 0 Comments
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 an URL 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:
<?
function fetch_page($url)
{
/* get hostname and path */
$host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_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
Like "Fetching a Web Page From Your PHP Code" ? Try Search It On:
»» Share4vn.com
»» Google.com
Most Popular Content
- Kopete, The KDE Instant Messenger
- Warning Signs You Can’t Ignore
- Robotic Jellyfish Swim and Fly at Hannover Fair
- The Real Indiana Jones
- Popular Wealth - Free Photoshop Brushes To Download
- 5 Ways To Combat Aging
- Inflatable robots could explore Mars
- Thinking the Way Animals Do
- Top 10 things to do in a blackout
- The most natural drug
- 30 Websites to follow if you’re into Web Development
- Please,I want you to help me with some of your books that will enable me to keep...
- You should also mention Ubercart (http://www.ubercart.org) a very powerful Drupa...
- Hello i am interessted on this script please can you contact me!?...
- Excellent article and oh so true.
Thanks...
- Well done.
If you use Ubuntu for, say, half a hour, you'll never go back to W...
- I own a "Super Battery" chinesse mobile phone, Chivak78, and I can assure you th...
- Thank You Very MUCH!!!
I had a working key, but the last time I reinstalled, it...
- Is this big deal end yet ?...
PHP, Tip & Tricks - May 30, 2008 3:20 - 0 Comments
Reuse Excel business logic with PHPExcel
More In Code
- Damn Small Rich Text Editor
- Make fancy buttons using CSS sliding doors technique
- Fetching a Web Page From Your PHP Code
- Quick Python Script Explanation
- 10 best CSS hacks
Photoshop Tips - May 31, 2008 8:20 - 0 Comments
Popular Wealth - Free Photoshop Brushes To Download
More In How to ?
- What do you think about Gif ?
- Publishing a VPN Server ”Walk-through
- Most used CSS tricks
- Download & Save Flash Animation Files in Firefox or IE
- The Best Security Pack For PC - Anti Virus Spyware Task And More

Leave a Reply