3.1: Using PHP
Create the Script
The PHP script we'll be writing will invoke our scraping session remotely, passing in a value for the TEXT_TO_SUBMIT session variable. Create a new PHP script on your computer, and paste the following code into it:
/**
* Note that in order to run this script the file
* remote_scraping_session.php must be in the same
* directory.
*/
require('remote_scraping_session.php');
// Instantiate a remote scraping session.
$session = new RemoteScrapingSession;
// Initialize the "Hello World" session.
echo "Initializing the session.<br />";
flush();
$session->initialize("Hello World");
// Put the text to be submitted in the form into a session variable so we can reference it later.
$session->setVariable("TEXT_TO_SUBMIT", "Hi everybody!" );
// Check for errors.
if($session->isError() )
{
echo "An error occurred: " . $session->getErrorMessage() . "<br />";
exit();
}
// Tell the session to scrape.
echo "Scraping <br />";
flush();
$session->scrape();
// Write out the text that was scraped:<
echo "Scraped text: " . $session->getVariable("FORM_SUBMITTED_TEXT") . "<br />";
// Very important! Be sure to disconnect from the server.
$session->disconnect();
// Indicate that we're finished.
echo "Finished.";
?>
Script Description
After creating our RemoteScrapingSession object we make a separate call to initialize it for our specific scraping session. After calling the Scrape method we check for any errors that may have occurred up to this point.
If for some reason your PHP script can't connect to the server you'd want to know before you tried to tell it to scrape.
Finally, we explicitly disconnect from the server so that it knows we're done.
OK, we're ready to give our script a try. Start screen-scraper running as a server.
Make sure that the remote_scraping_session.php file has been copied to the same directory as your PHP script (the file can be found in screen-scraper's installation directory, misc/php.
If you've succeeded in starting up the server go ahead and load your PHP script in a browser. After a short pause you should see the following in the browser output:
Scraping
Scraped text: Hi+everybody%21
Finished.
- Printer-friendly version
- Login or register to post comments