Sunday, September 30, 2007

Using output from other sites in your PHP scripts

Intended audience

This tutorial is intended for the PHP programmers who wish to use the output from other Web sites in their own scripts. A moderate understanding of PHP and HTML is assumed.

Overview

The example used in this tutorial is the automatic translation of phrases to multiple languages, to aid the development of multilingual applications.

When developing for the Internet, your audience will more than likely be from a variety of different countries and may not speak your language. You can easily adapt your own scripts to support multiple languages without speaking any of those languages yourself.

translate.dictionary.com is one of many Web sites offering translation services, and can be used within your own PHP scripts. Although the translations may not be perfect, they demonstrate the ability of your application to support multiple languages.

Objectives

In this tutorial you will learn how to:

  • Read the output from third-party Web sites in your own scripts
  • Send the correct information to Web sites to generate useful data
  • Identify the output format and decide how to extract the data you require
  • Utilize the data you have collected

Definitions

Target Web Page: The Web page from which data will be collected.

Background Information

This tutorial relies on the output from an external Web site. If and when the output from that site changes, modifications to any scripts that read information from the site may be required. If you are willing to spend a little time investigating how interactive Web sites work, the data they require and what they produce, you will often find that these sites produce useful data that you can use to enhance your own scripts.

Prerequisites

You may find it helpful to read the following sections of the PHP documentation:

  • Filesystem functions
  • Regular Expression Functions

How it works

First of all, visit the Web site http://translate.dictionary.com , where you will see the following form:

You need to find the data that this form collects to perform a translation. This particular page has a simple form and it is easy to pick out the elements you require. They are:

Your Target Web Page will therefore be http://translator.dictionary.com/fcgi/translate

To translate plain text, type or paste here:


Next you define a variable called 'text' which will contain your phrase:

Now you define a variable called 'lp' which will contain one of the values above.

You now have the information required to begin translating phrases for your own applications. The two variables $text and $lp must be supplied to http://translator.dictionary.com/fcgi/translate. This will generate a result that you can use in your own scripts.

Type a phrase into the text area and submit the form manually, the source of the resulting page contains:

tener diversión con PHP<-The translation of your phrase

The information returned can be examined and the translation extracted.

Sending the information to dictionary.com and reading the results

It's time to generate your first translation.

Code Flow

  • Send the correct information to dictionary.com and read the results
  • Check for the presence of your translation in the correct format
  • Return the translation or an error if something went wrong
PHP
function translate($phrase, $type){
$fp = @fopen("http://translator.dictionary.com/fcgi/translate?lp=$type&text=".urlencode($phrase), "r");
if(
$fp){
while(
$in = fread($fp, 1024))
$reply .= $in;
fclose($fp);
if(
ereg("(.*)", $reply, $reg))
return
trim($reg[1]);
else
return
"Error : Could not find translation!";
}
return
"Error : Could not connect to translate.dictionary.com!";
}

// translate a phrase from english to german
echo "First translation = ".translate("Having fun with PHP", "en_ge")."
\n"
;
?>


The output from the script is -> First translation = Spa? mit PHP haben

Now that you have the ability to translate phrases to multiple languages, you can store your translations in a database for use with your PHP scripts!

No comments: