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

Beginning PHP and Postgre SQL E-Commerce From Novice to Professional phần 10 ppsx

71 276 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 71
Dung lượng 1,36 MB

Nội dung

648XCH16.qxd 11/19/06 12:31 PM Page 546 Connecting to Web Services I n the dynamic world of the Internet, sometimes it isn’t enough to just have an important web presence; you also need to interact with functionality provided by third parties to achieve your goals. So far in this book, you already saw how to integrate external functionality to process payments from your customers. In this chapter, you’ll learn new possibilities for integrating features from an external source through a Web Service. A Web Service is a piece of functionality that is exposed through a web interface using standard Internet protocols such as HTTP. The messages exchanged by the client and the server are encoded using an XML-based protocol named SOAP (Simple Object Access Protocol) or by using REST (Representational State Transfer). These messages are sent over HTTP. You’ll learn more about these technologies a bit later. The beauty of using Web Services is that the client and the server can use any technology, any language, and any platform. As long as they exchange information with a standard proto- col such as SOAP over HTTP, there is no problem if the client is a cell phone, and the server is a Java application running on Solaris, for example. The possibilities are exciting, and we recommend you purchase a book that specializes in Web Services to discover more about their world. Have a look at the list of public Web Services at http://www.xmethods.net/ to get an idea of the kinds of external functionality you can inte- grate into your application. In this chapter, you’ll learn how to integrate the Amazon E-Commerce Service (ECS; Web Services interface provided by Amazon.com, formerly known as Amazon Web Services—AWS) to sell Amazon.com products through your HatShop web site. You already have an e-commerce web site that sells hats to its customers. You can go fur- ther and make some more money from their passion for hats by incorporating some other kinds of hats-related gifts from Amazon.com into your site. For free? Oh no . . . You’ll display Amazon.com’s details on your site, but the final checkout will be processed by Amazon.com, and Amazon.com will deliver in your bank account a small commission fee for purchases made from your web site. Sounds like easy money, doesn’t it? In this chapter, you’ll learn how to use ECS to add a special department called Amazon Super Hats to your web store, which you can see in Figure 17-1. This will be a “special” depart- ment in that it will be handled differently from others—for example, payment is handled directly by Amazon when the visitor wants to buy a product. This chapter explores just a small subset of ECS’s capabilities, so if you really want to make a fortune from this service, you should dig deeper to find more substance. 547 C H A P T E R 1 7 648XCH17a.qxd 11/22/06 5:41 AM Page 547 548 CHA P TER 1 7 ■ CONN ECTING TO W EB SERVIC ES Figure 17-1. Integrating the Amazon Super Hats department into HatShop The rest of the chapter is divided into two parts. In the first part, you’ll learn how to access the Amazon E-Commerce Service (ECS); in the second part, you’ll integrate ECS into the HatShop web site. ■ Ti p The code in this chapter is independent of the rest of the site, so all you need to get started integrat- ing Amazon functionality is the code from the first four chapters (so you have a working product catalog). Of course, with minor adjustments you can also adapt this code to your own personal solutions. 648XCH17a.qxd 11/22/06 5:41 AM Page 548 Accessing the Amazon E-Commerce Serv i c e Most service providers (including Amazon.com) use SOAP or REST (or both) to expose Web Services to Internet client programs. You can choose to make a Web Service request by using either REST or SOAP, and you get the exact same results with both options. In this chapter, you’ll learn how to access ECS 4.0 using both REST and SOAP. REST (Representational State Transfer) uses carefully crafted URLs with specific name- value pairs to call specific methods on the servers. You can find two useful articles about REST at http://www.xml.com/pub/a/2004/08/11/rest.html and http://www.onlamp.com/ pub/a/php/2003/10/30/amazon_rest.html. REST is considered to be the easiest way to communicate with the Web Services that expose this interface. Nonofficial sources say that 85% of ECS clients went the REST way. When using REST, all you have to do to perform an Amazon search is to make a classical HTTP GET request, and you’ll receive the response in XML format. SOAP (Simple Object Access Protocol) is an XML-based standard for encoding the infor- mation transferred in a Web Service request or response. SOAP is fostered by a number of organizations, including powerful companies such as Microsoft, IBM, and Sun. When accessing ECS, you can send the request either through REST or by sending a SOAP message. The Web Service will return an XML response with the data you requested. You’ll learn more about REST and SOAP by playing with ECS. ■ N o t e You need to understand that in this chapter we’ll touch just a bit of the functionality provided by the Amazon ECS. A serious discussion on the subject would probably need a separate book, but what you’ll see in this chapter is enough to get you on the right track. Also, be aware that in this chapter we integrate func- tionality from Amazon.com, but using the same Amazon ECS account, you can access services from Amazon.fr , Amazon.ca , Amazon.de , Amazon.co.jp , and Amazon.co.uk . Creating Your Amazon E-Commerce Service Account The official ECS web site is located at http://www.amazon.com/webservices. You can find the latest version of the documentation at http://developer.amazonwebservices.com/connect/— be sure to bookmark this URL because you’ll find it very useful. Before moving on, you need to create your account with the Amazon ECS. To access ECS, you need an Access Key ID, which identifies your account in the ECS system. If you don’t already have one, apply now at http://www.amazon.com/gp/aws/registration/ registration-form.html. The Access Key ID is a 20-character alphanumeric string. ■ N o t e Before October 11, 2005, Amazon used to provide something called a Subscription ID, instead of an Access Key ID. The purpose is similar, and if you already have a Subscription ID, you may continue using it. For any new applications, Amazon encourages you to use the Access Key ID. CH APTER 1 7 ■ CON NECTING TO WEB SERVI CES 549 648XCH17a.qxd 11/22/06 5:41 AM Page 549 The Access Key ID gives you access to more Amazon Web Services and Alexa Web Services (Alexa is a service owned by Amazon), as you can see in Figure 17-2. To access some of these services, you’ll also need a Secret Access Key, which you also get upon registration, but the Secret Access Key isn’t required when working with ECS. Figure 17-2. Amazon Web Services Obtaining an Amazon Associate ID T h e Access Key ID you created earlier is your key to re t r ieving data through the Amazon ECS. T h i s data allows you to compose the Amazon Super Hats department that you saw in Fi g u r e 17-1. CHA P TER 1 7 ■ CONN ECTING TO W EB SERVIC ES550 648XCH17a.qxd 11/22/06 5:41 AM Page 550 What the Access Key ID can’t do is give you a commission from the Amazon.com products that you sell through your web site. To obtain your money, you need to apply for an Associate ID. The Associate ID is used in the Buy From Amazon links you’ll display in your special Amazon department, and it’s the key that Amazon uses to identify you as the origin of that sale. The Associate ID can even be used in the static web pages that contain links to Amazon.com products, and it doesn’t require you to also have an ECS Access Key ID, which has different purposes. So before moving further, if you want to make any money out of your Amazon Super Hats department, go get your Associate ID from http://associates.amazon.com/gp/associates/ apply/main.html. Otherwise, if at the moment you’re just interested in learning about the ECS, feel free to skip this step now. Accessing Amazon E-Commerce Service Using REST REST Web Services are accessed by requesting a properly formed URL. Try the following link in your browser (don’t forget to replace the string [Your Access Key ID] with your real Access Key ID that you obtained earlier): http://webservices.amazon.com/onca/xml?Service=AWSECommerceService &AWSAccessKeyId=[Your Access Key ID] &Operation=ItemLookup &IdType=ASIN &ItemId=159059648X ■ Ti p Make sure you type the entire URL on a single line; we’ve broken it down to individual elements to make them easier to read. Your browser will display an XML structure with information about the book you are reading now. Figure 17-3 shows this XML structure in Firefox, which nicely displays the XML document tree. CH APTER 1 7 ■ CON NECTING TO WEB SERVI CES 551 648XCH17a.qxd 11/22/06 5:41 AM Page 551 Figure 17-3. The XML response of a Web Service request Pretty cool, huh? You have just seen REST in action. Every product in the Amazon data- base has a unique identifier called an ASIN (Amazon.com Standard Item Number). For books, the ASIN is the book’s ISBN (this book has the ASIN 159059648X). The Web Service request you just made tells ECS the following: I have an Access Key ID (AWSAccessKeyId=[Your Access Key ID]), and I want to make an item lookup operation (&Operation=ItemLookup) to learn more about the product with the 159059648X ASIN (&IdType=ASIN&ItemId=159059648X). You didn’t get much information about this book in this example—no price or availability information and no links to the cover picture or customer reviews. ECS 4.0 introduced a finer control of the data you want to receive using response groups (a response group is a set of information about the product). ■ N o t e At the time of writing, ECS offers a list of more than 35 possible response groups. In this book, we’ll only explain the purpose of the response groups we’re using for HatShop; for the complete list, visit the ECS documentation. CHA P TER 1 7 ■ CONN ECTING TO W EB SERVIC ES552 648XCH17a.qxd 11/22/06 5:41 AM Page 552 So let’s ask for some more data by using response groups. At the end of the link you’ve composed earlier, add the following string to get more specific information about the book: &ResponseGroup=Request,SalesRank,Small,Images,OfferSummary. The complete link should look like this: http://webservices.amazon.com/onca/xml?Service=AWSECommerceService &AWSAccessKeyId=[Your Access Key ID] &Operation=ItemLookup &IdType=ASIN &ItemId=159059648X &ResponseGroup=Request,SalesRank,Small,Images,OfferSummary The new XML response from Amazon.com includes more details about the Amazon.com item, as shown in Figure 17-4. Figure 17-4. The XML response of a Web Service request We have just mixed five response groups: Request, SalesRank, Small, Images, and OfferSummary. To learn more about the response groups, go to http://developer. amazonwebservices.com/connect/kbcategory.jspa?categoryID=5, and click the Latest Tech. Docs button. Alternatively, you can click the Technical Documentation link, and then click CH APTER 1 7 ■ CON NECTING TO WEB SERVI CES 553 648XCH17a.qxd 11/22/06 5:41 AM Page 553 the link of the latest documentation version. You can download the documentation in PDF format, or you can read it online here: http://docs.amazonwebservices.com/ AWSEcommerceService/2006-09-13/. In the ECS documentation, find the response groups details under the API Reference, Response Groups section. Here’s the description for the five response groups used in the previous example: • Request response group is a default response group in every kind of operation, and it returns the list of name-value pairs you used to make the request. • Sales Rank response group returns data about the current Amazon.com sales rank of the product. • Small response group returns general item data (ASIN, item name, URL, and so on) about items included in the response. This is a default response group for an Item- Lookup operation (like we have in this example). • Images response group gives you the addresses for the three pictures (small, medium, and large) for each item in the response. • OfferSummary response group returns price information for each item in the response. Let’s continue by learning how to make a REST request from PHP. To populate the future Amazon Super Hats department, you’ll search the Amazon.com Apparel department for the “super hats” keywords. One trivial way is to use the PHP file_get_contents function, as you can see in the following script. To test accessing Web Services using REST, create a new file named test_rest.php in your hatshop directory, and write the following code in it: <?php // Tell the browser it is going to receive an XML document. header('Content-type: text/xml'); /* DON'T FORGET to replace the string '[Your Access Key ID]' with your Access Key ID in the following line */ $url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService' . '&AWSAccessKeyId=[Your Access Key ID]' . '&Operation=ItemSearch' . '&Keywords=super+hats' . '&SearchIndex=Apparel' . '&ResponseGroup=Request,Medium'; echo file_get_contents($url); ?> CHA P TER 1 7 ■ CONN ECTING TO W EB SERVIC ES554 648XCH17a.qxd 11/22/06 5:41 AM Page 554 ■ N o t e Some PHP installations and web hosting providers may not allow this code to run by default. In that case, you can change this setting in php.ini: allow_url_fopen = On Alternatively, you can add the following line to include/config.php . This second solution is preferred because it only affects your application, and it remains set if you need to move the application to another server. ini_set('allow_url_fopen', 'On'); Loading http://localhost/hatshop/test_rest.php will show you XML data about Ama- zon’s Super Hats (see Figure 17-5). Figure 17-5. Super Hats from Amazon CH APTER 1 7 ■ CON NECTING TO WEB SERVI CES 555 648XCH17a.qxd 11/22/06 5:42 AM Page 555 [...]... INSTALLING APACHE, PHP, AND POSTGRESQL 6 For security reasons, PostgreSQL won’t allow root to start the server Execute the following command to change the owner of all the files to postgres: chown -R postgres:postgres /usr/local/pgsql 7 Log in as postgres, or use the su command, and then change the directory to /usr/local/pgsql: cd /usr/local/pgsql 8 Initialize a database cluster with this command: bin/initdb... in your browser to make sure your Apache Web Server is up and running, and then browse to https://localhost/ to test that you can also access Apache through SSL Installing PostgreSQL 8 Follow these steps to install PostgreSQL on your system: 1 Download the PostgreSQL source code from http://www.postgresql.org/ftp/source/ For the purposes of this installation guide, we’re using postgresql-8.1.5.tar.gz,... instead 2 Unpack the archive with a command such as the following: tar -zxvf postgresql-8.1.5.tar.gz 3 Change context to the postgresq-8.1.5 folder: cd postgresql-8.1.5 4 Execute the following command to prepare PostgreSQL to install to /usr/local/pgsql: /configure prefix=/usr/local/pgsql 5 Make sure you’re logged in as root (use the su command if necessary), and execute: make install 648XAppA.qxd... browser to http://localhost/test .php (or http://localhost:8080/ test .php if you installed Apache to work on port 8080) to test whether everything went okay with the installation You should get a PHP information page similar to Figure 2-7 from Chapter 2 575 648XAppA.qxd 576 11/19/06 1:45 PM Page 576 APPENDIX A ■ INSTALLING APACHE, PHP, AND POSTGRESQL Installing PostgreSQL To install PostgreSQL, follow... Copy php. ini-recommended from C: \PHP to your Windows folder, renaming it as php. ini 4 Uncomment the following lines from php. ini to enable the mhash, mcrypt, curl, pdo, and soap extensions If any of these lines isn’t present in the file, simply add it extension =php_ mhash.dll extension =php_ mcrypt.dll extension =php_ curl.dll extension =php_ pdo.dll extension =php_ pdo_pgsql.dll extension =php_ soap.dll 648XAppA.qxd... entries, and add the following lines (the names may vary depending on your specific Apache and PHP versions): LoadModule php5 _module c: /php/ php5apache2_2.dll AddType application/x-httpd -php php ■ Note If you don’t have the php5 apach2_2.dll file in your PHP folder, you can get it in the packages you can download from http://snaps .php. net/ 9 Also in httpd.conf, find the DirectoryIndex entry, and add index .php. .. libxml2 and libxml2-devel from http://xmlsoft.org/ and install them You can check for your libxml2 library with the following command: rpm -qa | grep libxml2 Compiling and Installing PHP 5 To compile and install PHP 5, follow these steps: 1 Go to the folder where you extracted the PHP source and execute the following commands: /configure with-config-file-path=/etc with-pdo-pgsql=/usr/local/pgsql/bin/pg_config... just finished your journey into learning about building e-commerce web sites with PHP and PostgreSQL You have the knowledge to build your own customized solutions, perhaps even more interesting and powerful than what we showed you in this book We hope you enjoyed reading this book, and we wish you good luck with your own personal PHP and PostgreSQL projects! 569 648XCH17a.qxd 11/22/06 5:42 AM Page 570... /data 9 Start PostgreSQL with this command: bin/pg_ctl -D /data -l data/logfile start 10 Now that PostgreSQL is started, you need to create a database and another user before going any further You should use a separate database for each of the projects to make things a little cleaner and easier to understand You should also use separate users for each database This keeps everything separate and “project... 648XAppA.qxd 11/19/06 1:45 PM Page 575 APPENDIX A ■ INSTALLING APACHE, PHP, AND POSTGRESQL 5 Also in php. ini, find this line: extension_dir = "./" and change it to extension_dir = "C: \PHP\ ext" 6 Copy libmhash.dll, libeay32.dll, ssleay32.dll, and libmcrypt.dll from your PHP folder to the Windows System32 folder 7 Open for editing, with any text editor (even Notepad), the Apache configuration file The default . Services—AWS) to sell Amazon.com products through your HatShop web site. You already have an e-commerce web site that sells hats to its customers. You can go fur- ther and make some more money from their. you have to do to perform an Amazon search is to make a classical HTTP GET request, and you’ll receive the response in XML format. SOAP (Simple Object Access Protocol) is an XML-based standard. script. To test accessing Web Services using REST, create a new file named test_rest .php in your hatshop directory, and write the following code in it: < ?php // Tell the browser it is going to

Ngày đăng: 12/08/2014, 14:21

TỪ KHÓA LIÊN QUAN