NEWS
» Sosovn News
» PHP News
» Articles
 
NEWSLETTER
Your Email:
 
READY TO BUY?
- Only $295 --> $245
- 06 months free upgrades
- Secure online ordering
- Download within 24 hours
 
Using output from other sites in your PHP scripts

By John Craig

Intended audience
Overview
Learning objectives
Definitions
Background information
Prerequisites
How it works

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:

<FORM ACTION="http://translator.dictionary.com/fcgi/translate" METHOD=POST NAME="trans_form" ONSUBMIT="return transcheck();">

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

<FONT FACE="Arial,Helvetica,sans-serif"><STRONG>To translate plain text</STRONG>, type or paste here:</FONT>

<BR>

<TEXTAREA NAME="text" ROWS="8" COLS="80" WRAP="Physical"></TEXTAREA>

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

<SELECT NAME="lp" SIZE="1" STYLE="font-size:110%; font-family: arial, helvetica, sans-serif;">

<OPTION VALUE="en_es"> English to Spanish

<OPTION VALUE="en_fr"> English to French

<OPTION VALUE="en_ge"> English to German

<OPTION VALUE="en_it"> English to Italian

<OPTION VALUE="en_pt"> English to Portuguese

<OPTION VALUE="es_en"> Spanish to English

<OPTION VALUE="fr_en"> French to English

<OPTION VALUE="fr_ge"> French to German

<OPTION VALUE="ge_en"> German to English

<OPTION VALUE="ge_fr"> German to French

<OPTION VALUE="it_en"> Italian to English

<OPTION VALUE="pt_en"> Portuguese to English

</SELECT>

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:

<!-- resultListStart -->

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

<!-- resultListEnd -->

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($fp1024))
          
$reply .= $in;
        
fclose($fp);
        if(
ereg("<!-- resultListStart -->(.*)<!-- resultListEnd -->"$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")."<br>\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!

About the Author

John Craig has been managing and developing e-commerce projects with PHP for many years. He is based in Paisley, Scotland and has recently founded a consulting company specializing in PHP development for business. You can contact him at mailto:jc@spl21.net.


« Back