Examples » Introduction

Posted June 17th, 2009 by admin

In this article, I will show you how to write HTTP scripts to solve a particular task. The examples given will show you the power of phpWebHacks, and its simplicity for HTTP scripting.

While HTTP scripts are relatively short, it takes efforts to write one. Usually you'll do the task manually with your browser and do a lot 'view HTML source'.

Here's an example. Say you want to login to your webmail. First, open your webmail in your browser, e.g: http://webmail.example.com. Your browser will display the login page.

Login page
Fig 1. Login page

Now switch to your PHP editor and type the following:

<?php
include 'phpWebHacks.php';

$h = new phpWebHacks;

$page = $h->get('http://webmail.example.com');

Back to your browser, right click to view the HTML source to see the form's action URL and the name of the fields. It may looks something like this:

<html>
<body>
<form action="redirect.php" method="post">
<table bgcolor="#ffffff" align="center" border="0" width="100%">
<tr>
  <td align="right" width="30%">Name:</td>
  <td align="left" width="*">
    <input type="text" name="login_username" value="" /> @
    <select name="login_domain">
      <option value="example.com">example.com
    </select>
  </td>
</tr>
<tr>
  <td align="right" width="30%">Password:</td>
  <td align="left" width="*">
    <input type="password" name="secretkey" />
  </td>
</tr>
</table>
<center><input type="submit" value="Login" /></center>
</form>
</body>
</html>

From the HTML source above, we find that the form's action is redirect.php, and the required fields are: login_username, login_domain, and secretkey. Switch to your HTTP script, 'fill in' your username and password, then login.

/* username & password */
$form['login_username'] = 'myusername';
$form['login_domain']   = 'example.com';
$form['secretkey']      = 'mypassword';

/* login */
$page = $h->post('redirect.php', $form);

The code above will return the HTML page for the authorized user, usually it is your inbox page. From this point, you can go to any page by requesting their URLs. Again, obtain the URLs from the HTML source.

The process is not easy for the first time, and you have to put a lot of efforts before getting a working script. But you'll get used to and it'll be a lot easier later. Here are more examples:

  1. Grabbing GMail Contact List
  2. Sending Emails with GMail
  3. Accessing del.icio.us Web Service API
  4. Bulk Image Downloader
  5. Upload Images to TinyPic and Retrieve the URLs
  6. Download FLV Videos from YouTube

Next »
Grabbing GMail Contact List

Leave a note

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000
captcha
Enter the code above: