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.

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

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测