1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P163 pps

5 83 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

  • Part II: Using MySQL

    • Chapter 7: Designing Your Web Database

    • Chapter 8: Creating Your Web Database

    • Chapter 9: Working with Your MySQL Database

    • Chapter 10: Accessing Your MySQL Database from the Web with PHP

    • Chapter 11: Advanced MySQL

  • Part III: E-commerce and Security

    • Chapter 12: Running an E-commerce Site

    • Chapter 13: E-commerce Security Issues

    • Chapter 14: Implementing Authentication with PHP and MySQL

    • Chapter 15: Implementing Secure Transactions with PHP and MySQL

  • Part IV: Advanced PHP Techniques

    • Chapter 16: Interacting with the File System and the Server

    • Chapter 17: Using Network and Protocol Functions

    • Chapter 18: Managing the Date and Time

    • Chapter 19: Generating Images

    • Chapter 20: Using Session Control in PHP

    • Chapter 21: Other Useful Features

  • Part V: Building Practical PHP and MySQL Projects

    • Chapter 22: Using PHP and MySQL for Large Projects

    • Chapter 23: Debugging

    • Chapter 24: Building User Authentication and Personalization

    • Chapter 25: Building a Shopping Cart

    • Chapter 26: Building a Content Management System

    • Chapter 27: Building a Web-Based Email Service

    • Chapter 28: Building a Mailing List Manager

    • Chapter 29: Building Web Forums

    • Chapter 30: Generating Personalized Documents in Portable Document Format (PDF)

    • Chapter 31: Connecting to Web Services with XML and SOAP

  • Part VI: Appendixes

    • Appendix A: Installing PHP and MySQL

    • Appendix B: Web Resources

  • Index

  • What’s On the CD-ROM?

Nội dung

782 Chapter 31 Connecting to Web Services with XML and SOAP } echo '<input type="hidden" name="tag-value" value="ASSOCIATEID">'; echo '<input type="hidden" name="tag_value" value="ASSOCIATEID">'; echo '<input type="image" src="images/checkout.gif" name="submit.add-to-cart" value="Buy From Amazon.com">'; echo ' When you have finished shopping press checkout to add all the items in your Tahuayo cart to your Amazon cart and complete your purchase.<br />'; echo '</form>'; echo '<a href = "index.php?action=emptycart"><img src = "images/emptycart.gif" alt = "Empty Cart" border = 0></a> If you have finished with this cart, you can empty it of all items. <br />'; echo '<h1>Cart Contents</h1>'; showSummary($products, 1, count($products), $mode, 0, true); } // show the small overview cart that is always on the screen // only shows the last three items added function showSmallCart() { global $HTTP_SESSION_VARS; echo '<table border = 1 cellpadding = 1 cellspacing = 0>'; echo '<tr><td class = cartheading>Your Cart $' .number_format(cartPrice(), 2). '</td></tr>'; echo '<tr><td class = cart>'.cartContents().'</td></tr>'; // form to link to an Amazon.com shopping cart echo '<form method="POST" action="http://www.amazon.com/o/dt/assoc/handle-buy-box">'; echo '<tr><td class = cartheading><a href = "index.php?action=showcart"><img src="images/details.gif" border=0></a>'; foreach($HTTP_SESSION_VARS['cart'] as $ASIN=>$product) { $quantity = $HTTP_SESSION_VARS['cart'][$ASIN]['quantity']; echo "<input type='hidden' name='asin.$ASIN' value='$quantity'>"; } echo '<input type="hidden" name="tag-value" value="ASSOCIATEID">'; echo '<input type="hidden" name="tag_value" value="ASSOCIATEID">'; Listing 31.14 Continued 37 525x ch31 1/24/03 3:35 PM Page 782 783 Solution Overview echo '<input type="image" src="images/checkout.gif" name="submit.add-to-cart" value="Buy From Amazon.com">'; echo '</td></tr>'; echo '</form>'; echo '</table>'; } // show last three items added to cart function cartContents() { global $HTTP_SESSION_VARS; $display = array_slice($HTTP_SESSION_VARS['cart'], -3, 3); // we want them in reverse chronological order $display = array_reverse($display, true); $result = ''; $counter = 0; // abbreviate the names if they are long foreach($display as $product) { if(strlen($product['name'])<=40) $result .= $product['name'].'<br />'; else $result .= substr($product['name'], 0, 37).' <br />'; $counter++; } // add blank lines if the cart is nearly empty to keep the // display the same for(;$counter<3; $counter++) { $result .= '<br />'; } return $result; } // calculate total price of items in cart function cartPrice() { global $HTTP_SESSION_VARS; $total = 0.0; Listing 31.14 Continued 37 525x ch31 1/24/03 3:35 PM Page 783 784 Chapter 31 Connecting to Web Services with XML and SOAP foreach($HTTP_SESSION_VARS['cart'] as $product) { $price = str_replace('$', '', $product['price']); $total += $price*$product['quantity']; } return $total; } // add a single item to cart // there is currently no facility to add more than one at a time function addToCart(&$cart, $ASIN, $mode) { if(isset($cart[$ASIN] )) { $cart[$ASIN]['quantity'] +=1; } else { // check that the ASIN is valid and look up the price $ars = new AmazonResultSet; $product = $ars->ASINSearch($ASIN, $mode); if($product->valid()) $cart[$ASIN] = array('price'=>$product->ourPrice(), 'name' => $product->productName(), 'quantity' => 1) ; } } // delete all of a particular item from cart function deleteFromCart(&$cart, $ASIN) { unset ($cart[$ASIN]); } ? > There are some differences about the way we do things with this cart. For example, look at the addToCart() function.When we try to add an item to the cart, we can check that it has a valid ASIN and look up the current (or at least, cached) price. The really interesting thing here is this question:When customers check out, how do we get their data to Amazon? Listing 31.14 Continued 37 525x ch31 1/24/03 3:35 PM Page 784 785 Installing the Project Code Checking Out to Amazon Look closely at the showCart() function in Listing 31.14. Here’s the relevant part: // build the form to link to an Amazon.com shopping cart echo '<form method="POST" action="http://www.amazon.com/o/dt/assoc/handle-buy-box">'; foreach($cart as $ASIN=>$product) { $quantity = $cart[$ASIN]['quantity']; echo "<input type='hidden' name='asin.$ASIN' value='$quantity'>"; } echo '<input type="hidden" name="tag-value" value="ASSOCIATEID">'; echo '<input type="hidden" name="tag_value" value="ASSOCIATEID">'; echo '<input type="image" name="submit.add-to-cart" value="Buy From Amazon.com">'; echo ' When you have finished shopping press checkout to add all the items in your Tahuayo cart to your Amazon cart and complete your purchase. <br />'; echo '</form>'; The checkout button is a form button that connects the cart to a customer’s shopping cart on Amazon.We send ASINs, quantities, and our Associate ID through as POST vari- ables.And hey presto! You can see the end result of clicking this button in Figure 31.5, back at the beginning of this chapter. The resulting page will look a little different if the user does not have an Amazon cookie stored on his machine allowing Amazon to identify him, but the end result is the same. One difficulty with this interface is that it is a one-way interaction.We can add items to the Amazon cart, but cannot remove them.This means that people cannot browse back and forth between the sites easily without ending up with duplicate items in their carts. Installing the Project Code If you want to install the project code from this chapter, you will need to take a few steps beyond the norm. After you have the code in an appropriate location on your serv- er, you will need to do the following: n Create a cache directory. n Set the permissions on the cache directory so that the scripts will be able to write in it. n Edit constants.php to provide the location of the cache. n Sign up for an Amazon developer tag. n Edit constants.php to include your developer tag and, optionally, your Associate ID. 37 525x ch31 1/24/03 3:35 PM Page 785 786 Chapter 31 Connecting to Web Services with XML and SOAP n Make sure NuSOAP is installed.We have it inside the Tahuayo directory, but you could move it and change the code. n Check that you have PHP compiled with XML support. Extending the Project There are lots of fun things you could do to extend this project: n You could expand the types of searches that are available via Tahuayo. n Amazon also has an XSLT Web Service that you might like to experiment with. n In Amazon’s Web Services How-To, there are links to innovative sample applica- tions. Look at these for more ideas: http://associates.amazon.com/exec/panama/associates/ntg/browse/-/567634/ Shopping carts are the most obvious thing to build with this data, but they are not the only thing. Further Reading There are a million books and online resources available on the topics of XML and Web Services. A great place to start is always at the W3C.You can look at the XML Working Group page: http://www.w3.org/XML/Core/ and the Web Services Activity page: http://www.w3.org/2002/ws/ just as a beginning. 37 525x ch31 1/24/03 3:35 PM Page 786 . action="http://www.amazon.com/o/dt/assoc/handle-buy-box">'; echo '<tr><td class = cartheading><a href = "index .php? action=showcart"><img src="images/details.gif". is valid and look up the price $ars = new AmazonResultSet; $product = $ars->ASINSearch($ASIN, $mode); if($product->valid()) $cart[$ASIN] = array('price'=>$product->ourPrice(),. tag. n Edit constants .php to include your developer tag and, optionally, your Associate ID. 37 525x ch31 1/24/03 3:35 PM Page 785 786 Chapter 31 Connecting to Web Services with XML and SOAP n Make

Ngày đăng: 07/07/2014, 03:20