【问题标题】:PHP / SOAP - trying to learn, but having a problem with implementationPHP / SOAP - 尝试学习,但在实施时遇到问题
【发布时间】:2012-12-24 06:37:11
【问题描述】:

我正在尝试自学 SOAP,只是为了扩展我的技能,但我碰壁了,我想知道那里的好心开发人员是否可以提供帮助?

我已经这样设置了我的服务器:

http://www.domain1.com/server.php

<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello',    // method name
 array('name' => 'xsd:string'),  // input parameters
 array('return' => 'xsd:string'), // output parameters
 'urn:hellowsdl',     // namespace
 'urn:hellowsdl#hello',    // soapaction
 'rpc',        // style
 'encoded',       // use
 'Says hello to the caller'   // documentation
);
// Define the method as a PHP function
function hello($name) {
        return 'Hello, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>

现在我尝试在单独的服务器上设置客户端:

http://www.domain2.com/client.php

<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the client instance
$client = new soapclient('http://domain.com/server.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
 // Display the error
 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
 // At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
 echo '<h2>Fault</h2><pre>';
 print_r($result);
 echo '</pre>';
} else {
 // Check for errors
 $err = $client->getError();
 if ($err) {
  // Display the error
  echo '<h2>Error</h2><pre>' . $err . '</pre>';
 } else {
  // Display the result
  echo '<h2>Result</h2><pre>';
  print_r($result);
 echo '</pre>';
 }
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

但我不能让这该死的东西工作。服务器显示得很好 - wdsl 输出很多。但是客户端不能/不会连接并完成交易。我收到这条消息:

Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in /home/public_html/slidebank_soap_client.php on line 5

Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient() [<a href='soapclient.soapclient'>soapclient.soapclient</a>]: Invalid parameters in /home/soap/slidebank_soap_client.php:5 Stack trace: #0 /home/soap/slidebank_soap_client.php(5): SoapClient->SoapClient('http://testing....', true) #1 {main} thrown in /home/public_html/soap/slidebank_soap_client.php on line 5

这就是我难过的地方......

有什么想法吗?

H

【问题讨论】:

    标签: php soap nusoap


    【解决方案1】:

    这个thread 可以帮助你,尤其是回答#9 和#10。

    【讨论】:

      【解决方案2】:

      我曾经很喜欢将 nusoap 与 PHP3/4 一起使用(即,当 PHP 没有内置 SOAP 客户端的时候)但使用 PHP5 的 SOAP 客户端,我发现这些天我不需要 nusoap。

      PHP5 内置 SOAP 客户端的一个优点是它实现起来更干净、更简单,因此提及它可能对您有帮助(我知道有些人选择使用 nusoap,但很多人显然不是请注意它在 PHP5 中,因为许多“HOWTO”指南已经过时并且忽略了提及它)。

      示例用法:

      ini_set("soap.wsdl_cache_enabled", "1"); // Set to zero to avoid caching WSDL
      $soapClient = new SoapClient('http://www.example.com/webservices/HelloWorldService/?wsdl");     
      $result = $soapClient->HelloWorldMethod(array('string' => "hello!"));    
      print_r($result);
      

      关于服务类型的说明:

      对于 SOAP 服务,理想情况下,您希望实现 Document/Literal 服务,而不是 RPC/Encoded 服务,因为 RPC/Encoded 是一种难以使用的格式,因此已被WS-I 支持 Document/Literal(使用起来更容易,设计上也更合乎逻辑)。

      首先,如果可以的话,您可能想尝试针对现有服务实施客户端 - 这样您就知道它至少可以正常工作,这可能会为您省去一些麻烦。

      不幸的是 - 虽然它非常擅长使用它们 - PHP 对 serving 文档/文字服务没有特别好的支持。这个 IIRC 至少有一个类似 nusoap 的第三方库,但对我来说并没有完全满足。

      (如有过时,欢迎指正。)

      【讨论】:

      • 感谢伊恩,这非常有用!最后我使用了我的服务代码和你的客户端代码,两者是无缝的!现在一切都已启动并运行,非常感谢:)
      • 很高兴您发现它很有用!看起来 nusoap (仍然)是目前使用 PHP 创建 SOAP 服务的最佳选择,所以看起来您已经走上了最佳路线。希望 PHP 会在某个时候获得一个内置的 SOAP 服务器实现,它和客户端一样优雅!
      • 是的,这似乎有点遗漏!为什么半吊子的工作,提供一个没有另一个?
      【解决方案3】:

      以防万一有些谷歌用户发现这个......

      我按照 ccheneson 的建议编辑了 php.ini,这段代码运行良好,即使在同一个开发服务器上也是如此。

      客户端 - 基于 PHP 模式的 Drupal 页面:

      <?php
      # URL for the service WSDL
      ini_set("soap.wsdl_cache_enabled", "0"); // Set to zero to avoid caching WSDL
      try {
          // Get a service proxy from the WSDL
          $proxy = new SoapClient('http://testing.domain.com/soap.php?wsdl');
          global $user;
          if (is_array($user->roles) && in_array('group1', array_values($user->roles))) {
              // User is logged in and is in the usergroup, perform login
              $id = $user->uid;
              $key = 1234; 
              $hashgenerator = $key . "xyz" . $id . "randomstringhere";
              $hash = sha1($hashgenerator);
              // Call a method on the service via the proxy
              $result = $proxy->handshake($id, $key, $hash);
              if ($hash==$result) {
                  //Send all user details
                  profile_load_profile($user);
                  $email = $user->mail;
                  $fname = $user->{profile_firstname};
                  $lname = $user->{profile_lastname};
                  $phone = $user->{profile_phone};
                  $fax = $user->{profile_fax};
                  $dept = "Speakers";
                  $role = "Speakers";
                  //Send request
                  $authresult = $proxy->authlogin($id, $email, $fname, $lname, $phone, $fax, $authgroup);
                  if ($authresult=='ok') {
                      //Logged in, show them the page
                      $_SESSION['loggedin'] = 1;
                  } else {
                      echo "Error 3";         
                  }
              } else {
                  echo "Error 2"; 
              }
          } else {
              echo "Error 1"; 
          }
      } catch(SoapFault $ex) {
          echo 'Error: ';
          if ($ex->getMessage() != '') {
              echo $ex->getMessage();
          } else {
              echo $ex . "\n";
          }
      }
      ?>
      

      服务:

      <?php
      //----------------------------------//
      // This file is a dummy service It obviously does nothing with the data, but i needed it to build my client scripts and so it might give you a hand returning the correct strings.
      //----------------------------------//
      
      // Pull in the NuSOAP code
      require_once('nusoap/nusoap.php');
      // Create the server instance
      $server = new soap_server();
      // Initialize WSDL support
      $server->configureWSDL('WDSL', 'urn:WDSL');
      // Register the methods to expose
      $server->register('handshake',              // method name
          array('id' => 'xsd:string', 
                'key' => 'xsd:string',
                'hash' => 'xsd:string'),          
          array('return' => 'xsd:string'),        // output parameters
          'urn:WDSL',                             // namespace
          'urn:WDSL#_handshake',                  // soapaction
          'rpc',                                  // style
          'encoded',                              // use
          'Initial handshake'                     // documentation
      );
      $server->register('authlogin',              // method name
          array('id' => 'xsd:string',             // User ID
                'email' => 'xsd:string',          // User email address
                'fname' => 'xsd:string',          // User first name
                'lname' => 'xsd:string',          // User last name
                'phone' => 'xsd:string',          // User phone
                'fax' => 'xsd:string',            // User fax 
                'dept' => 'xsd:string',           // SB department
                'role' => 'xsd:string'),          // SB role  
          array('return' => 'xsd:string'),        // output parameters
          'urn:WDSL',                             // namespace
          'urn:WDSL#authlogin',                   // soapaction
          'rpc',                                  // style
          'encoded',                              // use
          'Authentication of a user'              // documentation
      );
      // Define the method as a PHP function
      function slidebank_handshake($uid, $key, $hash) {
          //returns the same hash string for confirmation. No need to pass UID again.
          $hashgenerator = $key . "xyz" . $uid . "randomstringhere";  
          $hash = sha1($hashgenerator);
          return $hash;
      }
      // Define the method as a PHP function
      function slidebank_authlogin($id, $email, $fname, $lname, $phone, $fax, $dept, $role) {
          //logic to log user in and capture details etc
          return 'ok'; //options = ok or fail
      }
      // Use the request to (try to) invoke the service
      $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
      $server->service($HTTP_RAW_POST_DATA);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 2020-08-07
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        相关资源
        最近更新 更多