EvoTalk

06 十二月, 2007

PHP XML-RPC Client

Posted by: asd In: Code Snippet| Web| 程式設計 ()

server 端同上篇用 perl 寫的,client 端改用 php 來 call。

參考

PHP:
  1. /**
  2. * Debian: apt-get install php5-xmlrpc php5-curl
  3. */
  4.  
  5. /**
  6. The MIT License
  7. Copyright (c) 2007
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. *
  24. * @author: Tsung
  25. * @date: 2007/4/29
  26. * @version: v1.00
  27. *
  28. * @brief xmlrpc format generator
  29. *        XMLRPC format:
  30. *
  31. *
  32. *        $method_name
  33. *   *   *
  34. *           <$value_type>$value
  35. *
  36. *          *          *
  37. *
  38. *        USAGE:
  39. *          $re = encode_xmlrpc('echo', array( array('string' => 'Linux test')) );
  40. *          $xml = xmlrpc_request('http://localhost/', $re);
  41. *          $xml = xmlrpc_decode($xml);
  42. *
  43. *          $re = encode_xmlrpc('echo', array( 0 => array('string' => 'Linux test'), 1 => array('string' => 'test') ));
  44. *          $xml = xmlrpc_request('http://localhost/', $re);
  45. *          $xml = xmlrpc_decode($xml);
  46. *
  47. * @param method string:xmlrpc function name
  48. * @param params array: array( 0 => array('type', $value), 1 => array('type', $value) )
  49. * @retval xmlrpc_format/false
  50. */
  51. function encode_xmlrpc($method, $params)
  52. {
  53. $method = htmlspecialchars($method);
  54.  
  55. $response_pre = '';
  56. $response_pre.= "$method";
  57. $response_pre.= " ";
  58.  
  59. // xmlrpc spec: http://www.xmlrpc.com/spec, allow tag list
  60. $allow_type = array('i4', 'int', 'boolean', 'string', 'double', 'dateTime.iso8604', 'base64');
  61.  
  62. $response_mid = '';
  63. if (is_array($params)) {
  64. foreach ($params as $param) {
  65. foreach($param as $type => $val) {
  66. if(!in_array($type, $allow_type)) {
  67. return false;
  68. }
  69.  
  70. $val = htmlspecialchars($val);
  71. $response_mid .= " <$type>$val";
  72. }
  73. }
  74. }
  75.  
  76. $response_post = '';
  77.  
  78. return $response_pre . $response_mid . $response_post;
  79. }
  80.  
  81. /**
  82. * HTTP Post
  83. * @param url
  84. * @param postvar: args
  85. */
  86.  
  87. function postUrl($url, $postvar) {
  88.  
  89. $ch = curl_init();
  90. curl_setopt($ch, CURLOPT_URL, $url);
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  92. curl_setopt($ch, CURLOPT_POST, 1);
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar);
  94.  
  95. $res = curl_exec ($ch);
  96. curl_close ($ch);
  97.  
  98. return $res;
  99. }
  100.  
  101. /**
  102. * @brief xmlrpc request
  103. * @param url xmlrpc url
  104. * @param method string:xmlrpc function name
  105. * @param params array: array( 0 => array('type', $value), 1 => array('type', $value) )
  106. * @retval array/false
  107. */
  108. function request_xmlrpc($url, $method='', $params='') {
  109. if (empty($url) || empty($method) || empty($params)) {
  110. return false;
  111. }
  112.  
  113. $postvar = encode_xmlrpc($method, $params);
  114. $res = postUrl($url, $postvar);
  115. $res = xmlrpc_decode($res);
  116.  
  117. return $res;
  118. }
  119.  
  120. /* test */
  121.  
  122. $re = encode_xmlrpc('sum', array( 0 => array('int' => 2), 1 => array('int' => 6) ));
  123. $xml = postUrl('http://127.0.0.1:1234/RPC2', $re);
  124. $xml = xmlrpc_decode($xml);
  125. echo("answer:" + $xml);

Tags: ,

Releated Posts



No Responses to "PHP XML-RPC Client"

Comment Form