Fetching a Web Page From Your PHP Code

Started on the old forum in July 2011 · view the original in the archive · old links redirect here

1 reply · 646 views · started 15 years ago · 1 min read

2 participants
redrdx Developer · 80 posts #1

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.

EasyWebFetch - Fetch a web page from PHP code

EasyWebFetch is a class you use to fetch a web page 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 URL 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 chunked transfer-encoding used in HTTP 1.1
  • Support redirects
  • Support Proxies
  • Return headers and body or only headers. This might be useful for link checker applications.

Requirements

  • PHP 5
  • A working web server

Installation

Place EasyWebFetch.php in the directory containing your classes and include it in your code.

Common Usage

  1. Fetch a page

    <?php $wf = new EasyWebFetch; if (!$wf->get("http://www.google.com")) { print $wf->getErrorMessage(); exit; } $page = $wf->getContents(); ?>
  2. Check if a page exists

    <?php $wf = new EasyWebFetch; $wf->get("http://www.nashruddin.com/index.html", true); /* return headers only */ if ($wf->getStatus() == 200) { print "page exists."; } else { print "page missing."; } ?>
  3. Set maximum redirects. Default is 5.

    <?php $wf = new EasyWebFetch; $wf->setMaxRedirs(3); $wf->get("http://www.google.com"); ?>
  4. Examining the HTTP response headers

    <?php $wf = new EasyWebFetch; $wf->get("http://www.freebsd.org"); $headers = $wf->getHeaders(); print $headers['content_type']; print $headers['date']; /* print all */ print_r($headers); ?>
  5. Connect through a proxy

    <?php $wf = new EasyWebFetch; $wf->setProxyHost("proxy.its.ac.id:8080"); $wf->setProxyUser("myusername:mysecretpass"); $wf->get("http://www.google.com"); $page = $wf->getContents(); ?>

Contact

Please send comments and bug reports to jashanraju@gmail.com

Life On Clicks External image (may be gone) Get More Tricks Add Me On Gtalk ID Rdxmob@gmail.com

Attachments: easywebfetch.zip

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close