Use heredoc Style as an Alternative

Một phần của tài liệu How to Do Everything with Web 2.0 Mashups phần 8 docx (Trang 20 - 23)

As always, the code shown here is formatted with extra lines and indentation to make its structure clearer. You might want to use the heredoc style, described previously in Chapter 6, instead of the concatenation of strings shown here. That style means you do not have to worry about switching from the building of strings of HTML or JavaScript with interspersed PHP variables and single or double quotation marks. You would, however, need to move the amazonAssociateTag define value into a variable. For example, in the heredoc style, the code in the echo statement that follows would begin as

$amazonAssociateTag = amazonAssociateTag;

echo <<<EOT <iframe

src="http://rcm.amazon.com/e/cm?t=$amazonAssociateTag&

o=1&

p=8&

l=as1&

asins=$theASIN&

…more code EOT;

Use whatever style makes the most sense to you as you type, debug, and finalize it for maintenance in the future when other people will need to read it.

15

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

ResponseGroup=Request,Small&

Version=2007-02-22";

$theURL .= "&Style=xml";

$c = curl_init($theURL);

curl_setopt ($c, CURLOPT_RETURNTRANSFER, 1);

$xml = curl_exec($c);

curl_close($c);

$doc = new DOMDocument();

$doc->loadXML($xml);

$root = $doc->documentElement;

The curl code is standard, in all cases, whether you’re searching Amazon or accessing another site, but the next few lines are specific to Amazon. First, you get the Item elements of the returned document. Then, you loop through them looking for nodes with names Title, Author, and ASIN. Each of these is then passed into get_subnodes for echoing back for display. If you are using Amazon search, the following lines of code can be used without customization:

$theNodes = $root->getElementsByTagName('Item');

foreach ($theNodes as $theNode) {

get_subnodes ($theNode, 'Title', 'Title: ');

get_subnodes ($theNode, 'Author', 'Author(s): ');

get_subnodes ($theNode, 'ASIN', '', "useHref");

echo "<br>";

}

?>

This section of code is sufficient to perform the Amazon search and to display the results.

The next script calls createSearchControl with the keywords data from the submitted form.

That performs the Google search. Note, the mashup aspect of this is that the same data are passed into the Google and Amazon searches from the submitted form. (The reason the spaces in keywords need to be escaped for Amazon is that the text is embedded in a URL where spaces are not allowed. Here, the text of keywords is passed into a JavaScript function where spaces are allowed.)

<script language="Javascript" type="text/javascript">;

<?php

echo ("createSearchControl ('".$_REQUEST['keywords']."');");

?>

</script>

CHAPTER 15: Build a Mashup to Search Amazon and Google at the Same Time 235

The scripts for the Amazon search and for the Google search are presented in separate PHP code segments. This is for clarity—not just in the book, but in your own mashups.

The more you can keep the components of a mashup separate from one another, the easier it will be to reuse them.

Finally, you close the document with the standard include file, and your mashup page is complete.

<?php

include ('./Includes/PageBottom.html');

?>

Implement the Starting Page

The last step is to implement the start page, as shown in Figure 15-2.

You can place any graphics, instructions, links, or advertisements on the page. The key is the form that launches the mashup. Here is the HTML for the form:

<form action="chapter15.php"

method="post"

enctype="multipart/form-data"

name="Search Amazon">

<p>Keywords:

<input name="keywords"

type="text"

id="keywords"

FIGURE 15-2 Create a start page

15

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com

size="60">

</p>

<p>

<input type="submit" name="Submit" value="Search">

</p>

</form>

The only customization you need is to set the form action to the name of the PHP file, and to set the input field name and ID to the name you plan to use in the mashup to extract the data from the request.

Add Debugging and Error-Checking Code

As noted previously, the flow of the example code was not interrupted with debugging and error- checking code, so you can see how it works. In reality, of course, you need that code. Here are two general mechanisms for handling debugging and error checking. They are presented in the context of code used previously in this chapter.

In addition to these techniques, you can also use the approach shown in several chapters of this book, where you begin with a small part of the code and gradually add on new features so, at any given point, you have only a small number of new lines of code. For example, several of the mashups in this book begin by hard-coding items on which to search or even the data to be returned from a query. Once that works, you can add in the interface elements and the actual calls to a database or remote data source.

Một phần của tài liệu How to Do Everything with Web 2.0 Mashups phần 8 docx (Trang 20 - 23)

Tải bản đầy đủ (PDF)

(33 trang)