Creating and using web services with PHP

06 August 2009 Published in Blog

Basic Implementation of a Web Service client with PHP

A web service is an application that exposes or publish its functionalities or methods through an interface. It works in client-server mode, other applications talk to the web service using messages in a standardized format. Essentially, the communication is the same as a web server returning requested data by a browser such as an HTML page, the only difference is that instead of serving up HTML in response to HTTP requests, it serves up SOAP (XML) responses to SOAP requests.

Four elements make a web service:

 

  • XML

Standard format for exchange data primarily designed to the web. Its principal advantage is that a developer can define his own tags, this fix the way the data are read and validated by the client applications. XML schemas must be public so any change made in the application doesn’t affect communication. An xml file begins with this header:

  • SOAP

Acronym of Simple Object Access Protocol. Is a communication protocol between applications through messages, where web service objects will be instantiated and their methods will be requested. It was conceived to maintain independence of platform and language implementation, two applications created in different programming languages running on different operative systems, can request data and exchange information without know nothing about each other. SOAP is based on XML, that means that messages of applications are XML files with a predefined schema: SOAP messages must be contained in an envelope associated with a namespace by

SOAP messages must be contained in an envelope associated with a namespace by

xmlns:soap = "https://www.w3.org/2001/12/soap-envelope"

In this case the predefined standard is used:

https://www.w3.org/2001/12/soap-envelope

With encodingStyle, all the complex and simple data types used in the document are defined (any complex type formed with simple type elements can be defined and used). Header includes specific information about the message, like autetication. The message itself is inside the body.

  • WSDL

Web Service Description Language is a standard based on XML that describes access to the web service. It indicates wich are the interfaces, operations, messages and data type needed.

  • UDDI

Universal Discovery Description and Integration is a standard that describes the nature of a service, its capacities, location and requeriments in such a way that it can be categorized in web sites offering web service directory. UDDI uses WSDL to describe web service interfaces.

Some of the more common directories are:

https://soapclient.com/uddisearch.html

https://uddi.org/find.html

Applicability

The most used protocol used by web services is HTTP, not because web services can’t be mounted on other protocols such as FTP or SMTP, but because in an environment such as Internet firewalls block many times the access to other protocols.

Implementation

PHP has a few options for SOAP: a PEAR package, an independent and very popular library called NuSOAP and a PHP 5 extension. The last option will be used in this example. First of all native support must be enabled in the web server, in this example Apache is used. Find this lines in the PHP configuration file an uncomment them.

[soap]
; Enables or disables WSDL caching feature.
soap.wsdl_cache_enabled=1
; Sets the directory name where SOAP extension will put cache files.
soap.wsdl_cache_dir="/tmp"
; (time to live) Sets the number of second while cached file will be used
; instead of original one.
soap.wsdl_cache_ttl=86400

If we create a php script with phpinfo() function we will see something like this:

soap

Soap Client enabled

Soap Server enabled

Once this is done, the rest of the process is very simple

array(26) {
 [0]=> string(70) "ProductInfo KeywordSearchRequest(KeywordRequest $KeywordSearchRequest)"
 [1]=> string(79) "ProductInfo TextStreamSearchRequest(TextStreamRequest $TextStreamSearchRequest)"
 [2]=> string(64) "ProductInfo PowerSearchRequest(PowerRequest $PowerSearchRequest)"
 [3]=> string(79) "ProductInfo BrowseNodeSearchRequest(BrowseNodeRequest $BrowseNodeSearchRequest)"
 [4]=> string(61) "ProductInfo AsinSearchRequest(AsinRequest $AsinSearchRequest)"
 [5]=> string(75) "ProductLineArray BlendedSearchRequest(BlendedRequest $BlendedSearchRequest)"
 [6]=> string(58) "ProductInfo UpcSearchRequest(UpcRequest $UpcSearchRequest)"
 [7]=> string(58) "ProductInfo SkuSearchRequest(SkuRequest $SkuSearchRequest)"
 [8]=> string(67) "ProductInfo AuthorSearchRequest(AuthorRequest $AuthorSearchRequest)"
 [9]=> string(67) "ProductInfo ArtistSearchRequest(ArtistRequest $ArtistSearchRequest)"
 [10]=> string(64) "ProductInfo ActorSearchRequest(ActorRequest $ActorSearchRequest)"
 [11]=> string(85) "ProductInfo ManufacturerSearchRequest(ManufacturerRequest $ManufacturerSearchRequest)"
 [12]=> string(73) "ProductInfo DirectorSearchRequest(DirectorRequest $DirectorSearchRequest)"
 [13]=> string(83) "ListingProductDetails ExchangeSearchRequest(ExchangeRequest $ExchangeSearchRequest)"
 [14]=> string(76) "ProductInfo ListManiaSearchRequest(ListManiaRequest $ListManiaSearchRequest)"
 [15]=> string(73) "ProductInfo WishlistSearchRequest(WishlistRequest $WishlistSearchRequest)"
 [16]=> string(90) "SellerProfile SellerProfileSearchRequest(SellerProfileRequest $SellerProfileSearchRequest)"
 [17]=> string(68) "SellerSearch SellerSearchRequest(SellerRequest $SellerSearchRequest)"
 [18]=> string(88) "MarketplaceSearch MarketplaceSearchRequest(MarketplaceRequest $MarketplaceSearchRequest)"
 [19]=> string(79) "ProductInfo SimilaritySearchRequest(SimilarityRequest $SimilaritySearchRequest)"
 [20]=> string(83) "ShoppingCart GetShoppingCartRequest(GetShoppingCartRequest $GetShoppingCartRequest)"
 [21]=> string(89) "ShoppingCart ClearShoppingCartRequest(ClearShoppingCartRequest $ClearShoppingCartRequest)"
 [22]=> string(98) "ShoppingCart AddShoppingCartItemsRequest(AddShoppingCartItemsRequest $AddShoppingCartItemsRequest)"
 [23]=> string(107) "ShoppingCart RemoveShoppingCartItemsRequest(RemoveShoppingCartItemsRequest $RemoveShoppingCartItemsRequest)"
 [24]=> string(107) "ShoppingCart ModifyShoppingCartItemsRequest(ModifyShoppingCartItemsRequest $ModifyShoppingCartItemsRequest)"
 [25]=> string(118) "GetTransactionDetailsResponse GetTransactionDetailsRequest(GetTransactionDetailsRequest $GetTransactionDetailsRequest)"
 }

As you can see these are the methods exposed by the amazon web service.

Resources:

https://www.sitepoint.com/article/own-web-service-php-xml-rpc/

Serfe info(at)serfe.com https://www.serfe.com/images/serfe_logo_text.png https://www.serfe.com/images/serfe_logo_text.png FALUCHO 2032, S3016LDB, SANTO TOME, SANTA FE, ARGENTINA 1-305-5375397
Cookies & Privacy: 
This website uses cookies to ensure you get the best experience on our website.


Privacy Policy