Implementing a rest web service to implement a REST web Service is relatively simple, and the http get, POST, PUT, and DELETE features are used. The PHP processing code is very similar to the normal processing of POST and GET, and is converted to uppercase $ request_methodstrtoupper ($ _ SERVER [REQUEST_METHOD]); switch ($ request_met
Implementing a rest web service to implement a REST web Service is relatively simple, and the http get, POST, PUT, and DELETE features are used. The PHP processing code is very similar to the normal POST and GET processing code. // convert the code to uppercase $ request_method = strtoupper ($ _ SERVER ['request _ method']); switch ($ request_met
Implement a REST WEB
Service
Implement a REST webServiceThe http get, POST, PUT, and DELETE features are relatively simple. The PHP processing code is very similar to the common POST processing and GET processing code.
// Convert to uppercase $ request_method = strtoupper ($ _ SERVER ['request _ method']); switch ($ request_method) {case 'get': $ action = 'search '; break; case 'post': $ action = 'add'; break; case 'put': $ action = 'update'; break; case 'delete ': $ action = 'delete'; break; default: // invalid action exit ();} // results in XML or Json format are returned after processing
This code is used to process four different requests. The corresponding relationship between SQL and REST can be viewed in the table below.
SQL |
REST |
CREATE |
POST |
SELECT |
GET |
UPDATE |
PUT |
DELETE |
DELETE |
The book does not mention how to implement the access that requires authorization in rest. Currently, many open APIs require authorization before they can be used. The implementation principle is that each request requires an additional parameter, which is encrypted to verify the authorization information.
Provide data in SOAP Mode
Use the ext/soap SOAPServer class to implement WEB without WSDLService, You will find that the SOAPServer is used to build the WEBServiceIt is not very different from the common class used to write PHP (function can also be used), that is, to associate the class name or something with it when instantiating a SOAPServer. Below is a simple example.
class pc_SOAP_return_time { public function return_time() { return date('Ymd\THis'); }}$server = new SOAPServer(null, array('uri'=>'urn:pc_SOAP_return_time'));$server->setClass('pc_SOAP_return_time');$server->handle();
Client callServiceThe Code is as follows:
$opts = array('location' => 'http://api.example.org/getTime', 'uri' => 'urn:pc_SOAP_return_time');$client = new SOAPClient(null, $opts);$result = $client->__soapCall('return_time', array());print "The local time is $result.\n";
This is also introduced in the book that uses functions for processing, because I am full of OO and I will omit it.
If the client callsServiceWhen the method does not exist at the end,ServiceThe server uses a SOAP fault as a response. If you want to control the response content, you can use the _ call () method, as shown in the following code:
class pc_SOAP_Process_All_Methods { // Handle any undefined methods here public function __call($name, $args) { // ... }}$server = new SOAPServer(null, array('uri'=>'urn:pc_SOAP_Process_All_Methods'));$server->setClass('pc_SOAP_Process_All_Methods');$server->handle();
Accept parameters in the SOAP Method
InServiceThe client-side definition method is to add parameters, and then SOAPClient->__ soapCall ('Return _ time', array (parameter array) when the client is called; isn't it very easy, the above code can be changed to one place each.
Class pc_SOAP_return_time {// different from the first place, added the $ tz parameter public function return_time ($ tz = '') {if ($ tz) {$ my_tz = date_default_timezone_set ($ tz);} $ date = date ('ymd \ THis '); if ($ tz) {date_default_timezone_set (ini_get ('date. timezone ');} return $ date ;}$ server = new SOAPServer (null, array ('uris' => 'urn: pc_SOAP_return_time ')); $ server-> setClass ('pc _ SOAP_return_time '); $ server-> handle ();
Client call
$ Opts = array ('location' => 'HTTP: // api.example.org/getTime', 'uris '=> 'urn: pc_SOAP_return_time'); $ client = new SOAPClient (null, $ opts); // set tz to Oslo $ result = $ client->__ soapCall ('Return _ time ', array ('tz '=> 'Europe/Oslo'); print "The local time is $ result. \ n ";
Automatically generate the WSDL File
As mentioned above, ext/soap extensions do not support the auto-generated WSDL function. You can manually generate the WSDL file (I believe you will not do this ), in addition, you can use the following unofficial scripts. Note that these methods do not fully support SOAP and WSDL rules, if you want to use them well, you need to study them carefully.
WSDL_Gen, by George Schlossnagle
Http://www.schlossnagle.org /~ George/blog/index. php? /Archives/234-WSDLGeneration.html
Wsdl-writer, by Katy Coe based on code by David Griffin
Http://www.djkaty.com/drupal/php-wsdl
Web service helper, by David Kingma
Http://jool.nl/new/
Process SOAP header information
When ext/soap discovers a client request with a SOAP header, it first tries to call a function with the same name. After the call is complete, the function specified in the SOAP subject will be called. This allows you to execute any pre-requested transactions based on the SOAP header data.
However, ext/soapServiceThe tool does not really differentiate the SOAP header and the subject in programming. When ext/soap discovers a SOAP header, it tries to call a method with the same name as the Header element before processing the subject.
If the header specified by the SOAP client does not exist, ext/soap skips this method and directly transfers it to the subject. If the mustUnderstand attribute in the header is marked as true, the SOAPServer releases a SOAP fault.
$ Opts = array ('location' => 'HTTP: // api.example.org/getTime', 'uris '=> 'urn: pc_SOAP_return_time'); $ client = new SOAPClient (null, $ opts); $ set_timezone = new SOAPVar ('Europe/Oslo', XSD_STRING); // set the SOAP header $ tz = new SOAPHeader ('urn: pc_SOAP_return_time ', 'set _ timezone ', $ set_timezone); $ result = $ client->__ soapCall ('Return _ time', array (), array (), array ($ tz); print "The local time is $ result. \ n ";
Generate SOAP header information
Class pc_SOAP_return_time {public function return_time () {$ tz = Response (); // generate header information $ header = new SoapHeader ('urn: pc_SOAP_return_time ', 'Get _ timezone ', $ tz); $ GLOBALS ['server']-> addSoapHeader ($ header); return date ('ymd \ THis '); }}$ server = new SOAPServer (null, array ('uri '=> 'urn: pc_SOAP_return_time'); $ server-> setClass ('pc _ SOAP_return_time '); $ server-> handle ();
Because $ server object cannot be accessed easily in the scope of the method, you need to access this object through the $ GLOBALS array. The response will contain the following SOAP header:
America/Los Angeles
Use the SOAP header for verification
As there is no way to force ext/soap requests to the SOAP header information, you need to add a judgment statement in each method to determine pc_authenticate_user. If the client does not pass the authentication, a SOAP fault is thrown.
function pc_authenticate_user($username, password) { // authenticate user $is_valid = true; // Implement your lookup here if ($is_valid) { return true; } else { return false; }}class pc_SOAP_return_time { private $authenticated; public function __construct() { $this->authenticated = false; } public function authenticate_user($args) { // Throw SOAP fault for invalid username and password combo if (! pc_authenticate_user($args->username, $args->password)) { throw new SOAPFault("Incorrect username and password combination.", 401); } $this->authenticated = true; } // Rest of SOAP Server methods here... public function soap_method() { if ($this->authenticated) { // Method body here... } else { throw new SOAPFault("Must pass authenticate_user Header.", 401); } }}$server = new SOAPServer(null, array('uri'=>'urn:pc_SOAP_return_time'));$server->setClass('pc_SOAP_return_time');$server->handle();
The following code is called by the client:
$opts = array('location' => 'http://api.example.org/getTime', 'uri' => 'urn:pc_SOAP_return_time');$client = new SOAPClient(null, $opts);class SOAPAuth { public $username; public $password; public function __construct($username, $password) { $this->username = $username; $this->password = $password; }}$auth = new SOAPAuth('elvis', 'the-king');$header = new SOAPHeader('urn:example.org/auth', 'authenticate_user', $auth);$result = $client->__soapCall('return_time', array(), array(), array($header));