Tutorial 5: Page 4: The PHP File

The PHP File

Hopefully this is obvious, but we could just as easily use a web script written in ASP, Cold Fusion, or anything else that can be accessed via HTTP. In this tutorial we use PHP as an example simply because it's one of the most commonly used languages. Don't worry if you're not familiar with PHP, though, most of what we'll be going through is simple pseudo-code.

First try accessing the PHP directly here: http://www.screen-scraper.com/support/tutorials/tutorial5/db/save_product.php.

First, you'll notice that when the PHP script is accessed directly (i.e., not via a POST request) it simply displays a form that you can use to test it. Go ahead and try that now. Enter in some bogus information, then submit the form. If you entered in at least a title and a price you should get a small XML document that resembles the one you saw in screen-scraper. Go back to the form, leave the "Price" field blank, then re-submit the form. This time you get a message indicating that the data is incomplete.

When you submit the form you're taking exactly the same action that screen-scraper does when it invokes its "Save product" scrapeable file. The data is submitted to the PHP file via a POST request, validated, then inserted into the database.

We've simplified our example some to mostly pseudo-code. Take a look over the code for the PHP file here: http://www.screen-scraper.com/support/tutorials/tutorial5/db/save_product.php.txt. It's pretty heavily commented, so hopefully you can follow it even if you don't know PHP. Your code will obviously vary depending on the database you're using and any data validation you want to perform.

Having your web application return some kind of status message allows you to handle error conditions and such within screen-scraper. In this case you would probably want to set up an extractor pattern for the "Save product" scrapeable file that might look something like this:

<status>~@STATUS@~</status>

You might then write a script that does something special in the case of an error.