Php calls webservices in two ways: soap and xml-rpc

Source: Internet
Author: User
WebService introduction WebService is generated for the communication of heterogeneous systems. its basic idea is to provide a standard mechanism for remote calls using XML-based HTTP, this eliminates the need to establish a new protocol. At present, there are two protocol standards for WebService communication, one is XML-RPC and the other is SOAP. XML-RPC is relatively simple, appear earlier,

Web Service introduction

Web Service is generated for the communication of heterogeneous systems. its basic idea is to use XML-based HTTP remote calls to provide a standard mechanism, this eliminates the need to establish a new protocol. At present, there are two protocol standards for Web Service communication, one is XML-RPC, the other is SOAP. XML-RPC is relatively simple, the appearance of time is relatively early, SOAP is more complex, mainly in some need of stability, robustness, security and complex interaction when using.

I. how to use PHP XML-RPC to call Web services:

First, install the xmlrpc extension:

If the php extension of xmlrpc is not installed in your system, install it correctly.

On the Windows platform, first put the extension php_xmlrpc.dll under the PHP installation directory under the C: \ Windows or C: \ Winnt Directory (the extension of PHP4 is in the C: in the \ php \ extensions directory, PHP5 extensions are in the C: \ php \ ext directory) and C: \ Windows \ php. ini or C: \ Winnt \ php. remove the semicolon ";" in front of extension = php_xmlrpc.dll in ini, and restart the Web server and check if phpinfo () has a XML-RPC project to determine if the xmlrpc extension has been correctly installed.

On Unix/Linux platforms, if the xmlrpc extension is not installed, re-compile PHP and add the -- with-xmlrpc option to configure, and view phpinfo () check whether xmlrpc is installed properly.

(Note: The following operations are based on the normal installation of xmlrpc expansion. Be sure to install them correctly .)

XML-RPC working principle

XML-RPC is roughly the whole process is to use XML for communication. First, construct an RPC server to process the requests passed by the RPC client in XML format, and return the processing results to the RPC client in XML format, the client analyzes XML to obtain the data it needs.

The server side of the XML-RPC must have a ready-made function provided to the client to call, and the function and method in the request submitted by the client must be consistent with the server side, otherwise the desired result cannot be obtained. The server uses the xmlrpc_server_create function to generate a server, register the RPC calling interface to be exposed, accept the XML data POST from the RPC client, and process it, the processing result is displayed to the client in XML format.

Okay, I 've talked so much about it, and then we'll use an instance to see how to call Web serivces with XML-RPC protocols:

Server: server. php:

 ". $ Params [1]."
". $ Params [2];} else {$ return = "Not specify method or params";} return $ return ;} // Generate a XML-RPC of the server side $ xmlrpc_server = xmlrpc_server_create (); // register a server side call method rpc_server, actually pointing to the test function xmlrpc_server_register_method ($ xmlrpc_server, "rpc_server", "test"); // accept XML data from the client POST $ request = $ HTTP_RAW_POST_DATA; // Obtain the execution result after executing the XML request that calls the client $ xmlrpc_response = xmlrpc_server_call_method ($ xmlrpc_server, $ request, null );// Output header ("Content-Type: text/XML, charset = utf-8"); echo $ xmlrpc_response; // destroy the XML-RPC server-side resource xmlrpc_server_destroy ($ xmlrpc_server);?>

Client: client. php:

 "; // Construct the query POST request information for the XML-RPC server that requires communication $ query =" POST $ rpc_server HTTP/1.1 \ r \ nUser_Agent: XML-RPC Client \ r \ nHost :". $ host. "\ r \ nContent-Type: text/xml \ r \ nContent-Length :". strlen ($ request ). "\ r \ n ". $ request. "\ r \ n"; // echo $ query."
"; // Send the constructed HTTP protocol to the server. if it fails, false if (! Fputs ($ fp, $ query, strlen ($ query) {$ errstr = "Write error"; echo $ errstr ."
"; Return false;} // Obtain all information returned from the server, including the HTTP header and XML information $ contents =" "; while (! Feof ($ fp) {$ contents. = fgets ($ fp);} // after the connection resource is closed, the obtained content fclose ($ fp) is returned; return $ contents ;} // Construct the information of the RPC server. $ host = "localhost"; $ port = 80; $ rpc_server = "/xml/server. php "; // be sure to include/here. // Encode the XML request to be sent into XML. the method to call is rpc_server. the parameter is get $ request = xmlrpc_encode_request ("rpc_server", array ("get ", "My name is Adam", "I'm Li hantuan. "); // echo $ request; // call the rpc_client_call function to send all requests to the XML-RPC server and get the information $ response = rpc_client_call ($ host, $ port, $ rpc_server, $ request); write_file ($ response); // echo "aaa ". $ response. "xxx "."
"; // Analyze the XML returned from the server, remove the HTTP header information, and convert the XML into a string recognized by PHP $ split =" "; // Echo" split = ". $ split ."
"; $ Xml = explode ($ split, $ response); $ xml = $ split. array_pop ($ xml); // directly print echo $ xml to display the xml content; echo "returns the xml content:
". $ Xml; echo"
Convert the returned xml content to a string that PHP can recognize:
"; $ Response = xmlrpc_decode ($ xml);/* output information obtained from the RPC server */print_r ($ response);?>

In general, the above example is to submit a method called rpc_server. the parameter is get, and then get the server-side response. the XML data returned by the server is:

 
 
   
     
      
    
     Using POST method by client,post data is:
     
My name is Adam
我是李汉团.

Then, we can use the xmlrpc_decode function to encode the XML as a PHP string, so that we can process it at will and complete Web Service interaction.

Of course, sometimes we also need to parse the returned xml into an array. can this be done? Alternatively, use the following method to change client. php:

 "; // Construct the query POST request information for the XML-RPC server that requires communication $ query =" POST $ rpc_server HTTP/1.1 \ r \ nUser_Agent: XML-RPC Client \ r \ nHost :". $ host. "\ r \ nContent-Type: text/xml \ r \ nContent-Length :". strlen ($ request ). "\ r \ n ". $ request. "\ r \ n"; // echo $ query."
"; // Send the constructed HTTP protocol to the server. if it fails, false if (! Fputs ($ fp, $ query, strlen ($ query) {$ errstr = "Write error"; echo $ errstr ."
"; Return false;} // Obtain all information returned from the server, including the HTTP header and XML information $ contents =" "; while (! Feof ($ fp) {$ contents. = fgets ($ fp);} // after the connection resource is closed, the obtained content fclose ($ fp) is returned; return $ contents ;} // Construct the information for connecting to the RPC Server $ host = "localhost"; $ port = 80; $ rpc_server = "/xml/rpc_server.php"; // be sure to include /, do not. // Encode the XML request to be sent into XML. the method to call is rpc_server. the parameter is get $ request = xmlrpc_encode_request ("rpc_server", array ("get ", "My name is Adam", "I'm a php programmer"); // echo $ request; // call the rpc_client_call function to send all requests to the XML-RPC server side and get the information $ response = rpc_client_call ($ host, $ port, $ rpc_server, $ request); write_file ($ response ); // echo "aaa ". $ response. "xxx "."
"; // Analyze the XML returned from the server and remove the HTTP header information $ split =" "; $ Xml = explode ($ split, $ response); $ xml = array_pop ($ xml);/* echo:
". $ Xml; echo"
Convert the returned xml content to a string that PHP can recognize:
"; $ Response = xmlrpc_decode ($ xml); print_r ($ response); * // Convert XML into an array recognized by PHP: $ p = xml_parser_create (); xml_parse_into_struct ($ p, $ xml, $ vals, $ index); xml_parser_free ($ p); echo "\ nVals array \ n"; print_r ($ vals);?>

$ Value is the array converted from the returned xml. xml-rpc calls web services to parse the returned xml data.

II. how to use php soap to call web services:

 
 "Test"); // data returned from two-way translation between Chinese and English: Array $ result = $ client-> TranslatorString ($ parameters); echo"
";  print_r($result->TranslatorStringResult)."
"; echo "
"; // An example of an array containing sentences returned by two-way translation between Chinese and English: $ result1 = $ client-> Translator ($ parameters); echo"
";  print_r($result1->TranslatorResult)."
"; echo "
"; // Obtain candidate words: $ result2 = $ client-> SuggestWord ($ parameters); echo"
";  print_r($result2->SuggestWordResult)."
"; echo "
"; // Get the MP3 Byte stream and return data: Byte array Byte [] $ result3 = $ client-> GetMp3 ($ parameters); echo"
";  print_r($result3)."
"; echo "
"; * // * Example2: $ client = new SoapClient (" http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx? Wsdl "); $ param = array ('theipaddress' => '192. 96.134.33 '); $ result = $ client-> getCountryCityByIp ($ param); echo"
";  print_r($result->getCountryCityByIpResult);  echo "
"; $ Result1 = $ client-> getGeoIPContext ($ param); echo"
";  print_r($result1);  echo "
"; $ Result2 = $ client-> getVersionTime (); echo"
";  print_r($result2);  echo "
"; * // Example3: $ client = new SoapClient (" http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? Wsdl "); // obtain information about the province, region, and mobile phone card type of the mobile phone number in mainland China. $ parm = array ('lelecode' => '123 ', 'userid' => ''); $ result = $ client-> getMobileCodeInfo ($ parm); echo ($ result-> getMobileCodeInfoResult )."
"; // Obtain the information of the mobile phone number in mainland China database $ result1 = $ client-> getDatabaseInfo ($ parm); print_r ($ result1 )."
"; // Obtain the SOAP type list (Returns list of SOAP types) echo'
';print_r($client->__getTypes ()) ;echo '
'; // Obtain the webservice function echo'
';print_r($client->__getFunctions ()) ;echo '
'; // If the server does not support soap extensions, can I introduce open-source libraries on the Internet?>

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.