PHP and API tutorial (i)

Source: Internet
Author: User
Tags soap wsdl

Understand API :

Before using and creating your own API, we need to understand what api! is.

An API represents an application programming interface, and an interface is a common module of a particular service, an application, or other program.

Understanding SOA (Service-oriented architecture): SOA is an increasingly popular method of PHP applications that is based on a service layer system that provides all the functionality required by the system, but this feature provides an application layer that is not connected to the presentation layer. This allows multiple systems to use the same modular, reusable functionality. (Note: The two API-related books that I read about the API refer to this SOA, but it's not too much to understand because I'm a rookie.) So the implications for the above SOA excerpt from PHP Essentials-writing Efficient PHP code (US))

The use of the API is related to an important issue is the data format of the transfer: the main use of XML and JSON data in the API data transfer format. There is no explanation for these two data formats.

HTTP Hypertext Transfer Protocol when using the Protocol. The so-called access API simply means that the client accesses the server's defined program via HTTP, and further understands that it uses the programming code to get the data it needs via the HTTP request API. See the following simple process diagram:

Understanding Service Types:

RPC: Remote Procedurecall remotely calls the process, the RPC service is called the remote function and return the required data (most of the time need to pass in parameters, depending on the API). RPC is mainly divided into Xml-rpc and Json-rpc.

Soap: Simple Object Access Protocol, SOAP is a strictly defined XML-RPC. We typically use WSDL (Web Service Description Language) documentation to describe the SOAP service (that is, information that describes the method address provided by the service, and so on). including PHP in many languages has a built-in class library that makes it easy to handle soap and create and use SOAP services.

Rest:rest is not a protocol, it does not have a strictly defined interface and data format, more like a set of design principles. Rest treats everything as a resource, sending an HTTP verb to the appropriate URL through the client to make the resource transfer.

Simple examples of various services:

PHP and SOAP

Brief introduction:

PHP contains SOAP extensions that make it easier for us to build APIs that use soap types. Where soap is divided into WSDL type soap and non-WSDL type soap, most of the soap used today contains WSDL. The personal understanding with these two is that the WSDL is standard type, because WSDL uses the XML standard syntax to describe the method class that you provide (describe the service).

Since there is no functional function to generate the WSDL file for the method class in PHP, we need to use a different tool to get the WSDL file out. (Introduction to WSDL type)

Server: public soapserver::__construct ( mixed $wsdl [, array $options ])

Parameters: The first parameter is the necessary parameter, if soap for the WSDL type fills in the address of the WSDL (can be a relative address or web address)

Other parameters include:uri(命名空间)、location(服务器所在地址)、encoding(设置编码)、soap_version(设置soap服务版本 SOAP_1_1 or SOAP_1_2),其中前两个在非         WSDL中为必要参数

Client: public soapclient::soapclient ( mixed $wsdl [, array $options ])

Parameters: The first one is the necessary parameter

This extension is described in more detail in the official manual: http://php.net/manual/en/book.soap.php

1, start with a simple non-WSDL

First set up the Service method class namely: WebService.class.php

1 <? PHP 2 class WebService 3 {4      Public function Test () 5     {6         return ' webservice:test '; 7     }8 }

Second in the establishment of server.php provides access to the entrance

 1  <? php  2  include_once  './webservice.class.php ' Span style= "color: #000000";  3   $options  = array  (' uri ' = = ', ' location ' = ' '  4   $server  = New  SoapServer (null ,  $options  Span style= "color: #000000");  5   $server ->setclass (' WebService '); #   Fill in the class name  6   $server ->handle (); 

Last accessed: client.php

1 <? PHP 2 $options=array(3     ' uri ' = = ',4' location ' = '/     ' Www.test99.com/nowsdl/Server.php ',5    ); 6 $client=new soapclient (NULL,$options); 7 Echo $client->test ();

Access results:

Summary: Through various combinations of tests found, regardless of soapserver or soapclient parameters options, for non-WSDL: 1, the URI is necessary (even if it is empty) 2, the location of the soapserver is optional, The location of the soapclient is necessary and cannot be empty (the client has empty this data to locate the API)

(above are some of their own views, if there are deficiencies or errors please point out)

PHP and API tutorial (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.