PHP Web Server Instance

Source: Internet
Author: User
Tags soap php web server wsdl

With WebService, we can invoke programs that are deployed elsewhere without worrying about what platform the program was written in. Here we use PHP calls.

In the PHP4 era call WebService most of the nusoap used, to the PHP5 already have their own SOAP extension, we just need to open the extension in the PHP configuration file php.ini

Extension = Php_soap.dll

  

Instance Program

Define a class file that implements the business logic basic.php

The test definition exposes classes class MYWSDL {    private $nombre = ';    Public function __construct ($name = ' world ') {$this->name = $name;}    Public function greet ($name = ") {$name = $name? $name: $this->name;        Return ' Hello '. $name. '. ';}    Public Function Servertimestamp () {return time ();} Public Function myfunc ($a = ") {    $arr = array (' a ' = = $a, ' b ' = = $a, ' c ' = = $a);        Return Json_encode ($arr);}}

A class that returns a string. Suppose this program is above server A, what if you want to call this program on Server B? First, we need to make this Class A Web service, which requires the SOAP extension of the previous PHP.

To create a service server.php, you need to use the SoapDiscovery.class.php file

Require_once (' basic.php ');//define (' Wsdl_url ', ' hello.wsdl ');        Defines the WSDL file path Ini_set (' soap.wsdl_cache_enabled ', ' 0 ');  automatically create if (!file_exists (' mywsdl.wsdl ') {    require_once ' SoapDiscovery.class.php ') when the WSDL cache//wsdl file does not exist;    $disco = new Soapdiscovery (' mywsdl ', ' www.zhuoqu.com ');    

OK, a Web service is set up, in the example above, we created a SoapServer object, and after that, we use the SetClass function to put the basic class that we just wrote in, and finally handle (). Now if you use a browser to access this file, a warning will appear, which is normal, because the file is not accessed by the browser, but is accessed by another program. When a client program accesses our webservice, running to handle () will process the client input and output the results to the client, where it is important to note that you do not output anything before and after handle (), and the client will not be able to handle it.

Now, write client access to our WebService

 

$client = new SoapClient ("http://127.0.0.1/s.php?wsdl"), try {$result = $client->myfunc (' 789 '); Var_dump (Json_ Decode ($result)); echo "The answer is result";} catch (SoapFault $e) {echo "Error Message: {$e->getmessage ()}";}

So we can access the client files to get the results we need.

SoapDiscovery.class.php file

    

<?php/** * Copyright (c) 2005, Braulio Jos? Solano Rojas * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, is * permitted provided that the FO Llowing conditions is met: * * redistributions of source code must retain the above copyright notice, this list of * con  Ditions and the following disclaimer. * redistributions in binary form must reproduce the above copyright notice, this list of * conditions and the following di  Sclaimer in the documentation and/or and materials * provided with the distribution.  * Neither the name of the Solsoft de Costa Rica S.A. and the names of its contributors may * is used to endorse or promote Products derived from this software without specific * prior written permission. * * This software are provided by the COPYRIGHT holders and * CONTRIBUTORS ' as is ' and any EXPRESS OR implied warranties, * including, LIMITED to, the implied warranties of * merchantability and FITNESS for A PARticular PURPOSE is * disclaimed. In NO EVENT shall the COPYRIGHT OWNER OR * CONTRIBUTORS is liable for any DIRECT, INDIRECT, incidental, * special, EXEMPLA  RY, or consequential damages (including, but not LIMITED to, procurement of substitute GOODS OR SERVICES; * LOSS of Use, DATA, OR profits; or business interruption) * However caused and on any theory of liability, WHETHER in * contract, STRICT liability, OR TOR T (including negligence OR * OTHERWISE) arising in any-out-of-the---the-software, * Even IF advised of the Possi Bility of SUCH DAMAGE. * * * @version $Id $ * @copyright 2005 *//** * Soapdiscovery Class that provides Web Service Definition Language (WSDL). * * @package soapdiscovery * @author Braulio Jos? Solano Rojas * @copyright Copyright (c) 2005 Braulio Jos? Solano Rojas * @version $Id $ * @access public **/class soapdiscovery {private $class _name = ';p rivate $service _name = '; /** * SOAPDISCOVERY::__CONSTRUCT () Soapdiscovery class Constructor. * * @param string $class _name * @param string $service _name **/public function __construct ($class _name = ', $service _name = ') {$this-& Gt;class_name = $class _name; $this->service_name = $service _name;} /** * SOAPDISCOVERY::GETWSDL () Returns The WSDL of a class if the class is instantiable. * * @return string **/public function getwsdl () {if (Empty ($this->service_name)) {throw new Exception (' No service name .‘);} $headerWSDL = "<?xml version=\" 1.0\ ">\n"; $headerWSDL. = "<definitions name=\" $this->service_name\ " Targetnamespace=\ "urn: $this->service_name\" xmlns:wsdl=\ "http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\ "http ://schemas.xmlsoap.org/wsdl/soap/\ "xmlns:tns=\" urn: $this->service_name\ "xmlns:xsd=\" http://www.w3.org/2001/ Xmlschema\ "xmlns:soap-enc=\" http://schemas.xmlsoap.org/soap/encoding/\ "xmlns=\" http://schemas.xmlsoap.org/wsdl /\ ">\n"; $headerWSDL. = "<types xmlns=\" http://schemas.xmlsoap.org/wsdl/\ "/>\n"; if (Empty ($this->class_ Name) {throw new Exception (' NoClass name. ');} $class = new Reflectionclass ($this->class_name), if (! $class->isinstantiable ()) {throw new Exception (' class is not Instantiable. ');} $methods = $class->getmethods (); $portTypeWSDL = ' <porttype name= '. $this->service_name. ' Port ">"; $bindingWSDL = ' <binding name= "'. $this->service_name. ' Binding "Type=" TNS: '. $this->service_name. ' Port\ ">\n<soap:binding style=\" rpc\ "transport=\" http://schemas.xmlsoap.org/soap/http\ "/>\n"; $ servicewsdl = ' <service name= '. $this->service_name. " \ ">\n<documentation/>\n<port name=\" ". $this->service_name. ' Port "binding=" TNS: '. $this->service_name. ' binding\ "><soap:address location=\"/http ". $_server[' server_name ']. ': '. $_server[' Server_port '].$_SERVER[ ' Php_self ']. " \ "/>\n</port>\n</service>\n"; $messageWSDL = "; foreach ($methods as $method) {if ($method->ispublic () && $method->isconstructor ()) {$portTypeWSDL. = ' <operation name= "'. $method->getname ()." \"&Gt;\n "." <input message= "TNS: '. $method->getname ()." Request\ "/>\n<output message=\" TNS: ". $method->getname ()." Response\ "/>\n</operation>\n"; $bindingWSDL. = ' <operation name= '. $method->getname (). " \ ">\n". ' <soap:operation soapaction= "urn: '. $this->service_name. ' # '. $this->class_name. ' # '. $method->getname (). " \ "/>\n<input><soap:body use=\" encoded\ "namespace=\" urn: $this->service_name\ "encodingStyle=\" http ://schemas.xmlsoap.org/soap/encoding/\ "/>\n</input>\n<output>\n<soap:body use=\" encoded\ " Namespace=\ "urn: $this->service_name\" encodingstyle=\ "http://schemas.xmlsoap.org/soap/encoding/\"/>\n<    /output>\n</operation>\n "; $messageWSDL. = ' <message name= '. $method->getname (). " Request\ ">\n"; $parameters = $method->getparameters (); foreach ($parameters as $parameter) {$messageWSDL. = ' < Part Name= "'. $parameter->getname ()." \ "Type=\" xsd:string\ "/>\n";} $messageWSDL. = "</message>\ n "; $messageWSDL. = ' <message name=" '. $method->getname (). " Response\ ">\n"; $messageWSDL. = ' <part name= '. $method->getname (). " \ "Type=\" xsd:string\ "/>\n"; $messageWSDL. = "</message>\n";}}        $portTypeWSDL. = "</porttype>\n"; $bindingWSDL. = "</binding>\n"; /** * 2017-01-16 17:35:12 * Author SUR * * $file = fopen ($this->class_name. WSDL ', ' W '); fwrite ($file, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $        MESSAGEWSDL, ' </definitions> '); Fclose ($file);} /** * soapdiscovery::getdiscovery () Returns discovery of WSDL. * * @return string **/public function Getdiscovery () {return "<?xml version=\" 1.0\ "? >\n<disco:discovery xmlns:d Isco=\ "http://schemas.xmlsoap.org/disco/\" xmlns:scl=\ "http://schemas.xmlsoap.org/disco/scl/\" &GT;\N&LT;SCL: Contractref ref=\ "/http". $_server[' server_name ']. ': '. $_server[' server_port '].$_server[' php_self ']. "? Wsdl\ "/>\n</disco:discovery>";}}? >

  

SOURCE HTTP://WWW.OPEN-OPEN.COM/DOC/VIEW/FB5AF38FEFF2470DA22458E31C2087B1

Http://www.360doc.com/content/11/0212/16/15103_92457365.shtml

PHP Web Server Instance

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.