douluan1533 2015-09-07 22:57
浏览 28
已采纳

PHP和SOAP集成

I connected to a SOAP server from a client and am trying to send form information back.

The connection works, but I have no idea how to send data back. I have received documentation ( -> http://s000.tinyupload.com/?file_id=89258359616332514672) and am stuck at the function AddApplication

This is the PHP code I've written so far. There is no form integration yet, only dummy data.

    <?

    $client = new SoapClient(
        'https://wstest.hrweb.be/TvBastards/TvBastards/Job.svc?singleWsdl',
        array(
            'soap_version' => SOAP_1_1
        )
    );

    $params = array(
        'username' => 'XXX',
        'password' => 'XXX',
        'environmentKey' => 'XXX',
    );

    //Open session
    try{
        $token = $client->OpenSession($params);
    }catch(SoapFault $ex){
        echo "<pre>";
        print_r($ex->detail->ExceptionDetail);
        echo "</pre>";
    }

    //Add Application
    try{
        $resp = $client->AddApplication($params, ___THE_XML_SHOULD_BE_HERE___); // I have no idea how I can implement a XML file over here, and make this part work
    }catch(SoapFault $ex){
        echo "<pre>";
        print_r($ex->detail->ExceptionDetail);
        echo "</pre>";
    }

    //Close session
    try{
        $app = $client->CloseSession($token);
    }catch(SoapFault $ex){
        echo "<pre>";
        print_r($ex);
        echo "</pre>";      
    }`

The error I receive now is the following:

End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 156.

Which is understandable as I don't provide any XML.

I receive my token so the OpenSession works perfectly. As said, I'm completely stuck at the AddApplication function. This is my first encounter with a SOAP service, so every possible bit of explanation is highly appreciated.

  • 写回答

1条回答 默认 最新

  • douyueqing1530 2015-10-06 07:52
    关注

    Fixed it, and hopefully it can help out some others. I'll try and put it into steps.

    define('SIM_LOGIN', 'LOGIN NAME HERE');
    define('SIM_PASSWORD', 'LOGIN PASSWORD HERE');
    define('ENV_KEY', 'ENVIRONMENT KEY HERE');
    
    /*** login parameters ***/
    $params = array(
      'username' => SIM_LOGIN,
      'password' => SIM_PASSWORD,
      'environmentKey' => ENV_KEY,
    );
    
    /*** Set up client ***/
    $client = new SoapClient(
      __SOAP URL HERE__,
      array(
        'soap_version' => SOAP_1_1
      )
    );
    

    After setting up the parameters and connecting to the client, we can start calling functions within the SOAP service. Every SOAP service will be different, so function names and parameters can be different. In below example I need to open a session to retrieve a token. This token is used in all the other functions, so this function is necessary. If something fails I call the "abort()" function.

    try{
      $token = $client->OpenSession($params);
    }catch(SoapFault $ex){
      abort();
    }
    

    If the token is received I call upon the function AddApplication. This expects the token parameter and an "object" (which is basically an STDClass).

    I create an stdClass with all my data:

    /*** Create stdClass with requested data ***/
    $std = new stdClass();
    
    $std->Firstname           = $firstname;
    $std->Lastname            = $lastname;
    $std->Birthdate           = $birthdate;
    $std->Phone               = $phone;
    $std->Email               = $email;
    

    Be sure to check camelcasing of names or capitals, as this makes all the difference.

    Now we call upon the AddApplication function with parameters "token(string)" and "application(object)".

    /*** AddApplication ***/
    try{
      $result = $client->AddApplication(array("token" => $token, "application" => $std));
    }catch(SoapFault $ex){
        abort();
    }
    

    If all goes well the data is stored on the external server and you receive a "success" message. It's possible that you receive a "fail" even without going into the SoapFault. Be sure to log both "$result" and "$ex", as a SOAP service can return a "Fail" but the try-catch sees this as a well formed result.

    Last thing to do is close the session (and destroy the token)

    /*** CloseSession ***/
    try{
      $app = $client->CloseSession($token);
    }catch(SoapFault $ex){
      abort();
    }
    

    If any questions, don't hesitate to ask them here, I'll be glad to help as I had such problems figuring this out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成