PHP 5.3 SOAP请求是如何形成的? [英] How is PHP 5.3 SOAP request formed?

查看:124
本文介绍了PHP 5.3 SOAP请求是如何形成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试写一些code去跟SmarterMail API,但我似乎无法使PHP格式的要求正确。在[丑] code我至今是:

 < PHP
功能array_to_params($ in_arr){
    $ out_arr =阵列();

    的foreach($ in_arr为$关键=> $值){
        $ out_arr [] =新SoapParam($值,$键);
    }
    返回$ out_arr;
}

回声< pre>中;

$ SoapClient的=新SoapClient的(
    http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL
    阵列('跟踪'=>真)
);

后续代码var_dump($ soapClient-> __ getTypes());模具();

$ soap_user_params =阵列(
    '空白'=> '空白',//如果我没有一个虚拟条目的第一个参数显示不出来。
    AuthUserName'=> 管理员,
    AuthPassword'=> DERP
);
$ soap_user_params = array_to_params($ soap_user_params);

$错误= FALSE;
尝试 {
    $信息= $ soapClient-> __的SOAPCall(GetAllDomains,$ soap_user_params);
// $信息= $ soapClient-> GetAllDomains($ soap_user_params); //相同的结果
}赶上(的SOAPFault $故障){
    $错误= TRUE;
    的printf(错误%S:%S \ N,$容错>故障code,$容错>在faultstring);
}

如果(!$错误){
    后续代码var_dump($信息);
    回声\ nRequest:。ヶ辆($ soapClient-> __ getLastRequest());
}

回声< / pre>中;
?>
 

有关参考,WSDL此功能是:

 < S:元素的名称=GetAllDomains>
    < S:复杂类型>
        &所述氏:序列GT;
            &所述氏:元件的minOccurs =0maxOccurs为=1名称=AuthUserName类型=S:串/>
            &所述氏:元件的minOccurs =0maxOccurs为=1名称=AuthPassword类型=S:串/>
        &所述; /秒:序列GT;
    < / S:复杂类型>
&所述; /秒:组件>
 

给出的例子的要求是:

 < XML版本=1.0编码=UTF-8&GT?;
<肥皂:信封的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema的xmlns:肥皂= http://schemas.xmlsoap.org/soap/envelope/">
  <肥皂:身体与GT;
    < GetAllDomains的xmlns =htt​​p://tempuri.org/>
      < AuthUserName>字符串< / AuthUserName>
      < AuthPassword>字符串< / AuthPassword>
    < / GetAllDomains>
  < / SOAP:身体与GT;
< /肥皂:信封>
 

但是,我的code产生的请求是:

 < XML版本=1.0编码=UTF-8&GT?;
< SOAP-ENV:信封的xmlns:SOAP-ENV =htt​​p://schemas.xmlsoap.org/soap/envelope/的xmlns:NS1 =htt​​p://tempuri.org/>
    < SOAP-ENV:身体与GT;
        < NS1:GetAllDomains />
        < AuthUserName>管理< / AuthUserName>
        < AuthPassword> DERP< / AuthPassword>
    < / SOAP-ENV:身体与GT;
< / SOAP-ENV:信封>
 

可以看到,用户名和密码参数中不包含由于某种原因GetAllDomains部内。同时,在code,你可以看到,我要传递的第一个虚拟的参数,因为不管是第一次没有请求出现。另外,我不能简单地传递一个关联数组,因为密钥不被用作参数名称,它把在参数1'和'参数2'代替。

我应该注意的是,服务器接受请求,并吐回一个关于身份验证失败的消息。在服务器上的内置的SOAP请求测试仪,并且当请求符合给定的例子它正常工作

一切我已经看到了在PHP文档,并通过谷歌似乎表明,它不应该是这个困难。 :(

任何帮助是极大AP preciated。

请注意:我在FreeBSD上运行PHP 5.3.8在Apache 2.2,建成并通过端口安装今天

后的答案编辑:

由于吉安的回答下面我已经得到了这方面的工作,大大简化了code:

 < PHP

回声< pre>中;

$ SoapClient的=新SoapClient的(http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL,阵列('跟踪'=>真));

$ soap_user_params =阵列(
    AuthUserName'=> 管理员,
    AuthPassword'=> DERP
);

尝试 {
    $信息= $ soapClient-> __的SOAPCall(GetAllDomains,阵列('参数'=> $ soap_user_params));
    后续代码var_dump($信息);
    回声\ nRequest:\ N。ヶ辆($ soapClient-> __ getLastRequest());
}赶上(的SOAPFault $故障){
    的printf(错误%S:%S \ N,$容错>故障code,$容错>在faultstring);
}

回声< / pre>中;
 

解决方案

我道歉的错误指向接近。我误解你试图做在第一。

通过的SOAPCall 文档注释过筛,有一对夫妇的事情,我会建议尝试。首先,这样的:

  $ soap_user_params =阵列(
    '参数'=>阵列(
        AuthUserName'=> 管理员,
        AuthPassword'=> DERP
    )
);
 

显然,你的参数需要在一个关联数组键'参数'的元素。奇怪,是吧?

如果做不到这一点,你可能需要这个窝更深!该 _ 数组项未出现被记录任何地方显而易见的,但是这似乎是东西,在PHP文档评论者被建议。因此,也许这样的:

  $ soap_user_params =阵列(
    '参数'=>阵列(
        AuthUserName'=>阵列('_'=>管理员),
        AuthPassword'=>阵列('_'=>DERP)
    )
);
 

I am trying to write some code to talk to the SmarterMail API, but I can't seem to make PHP format the request properly. The [ugly] code I have so far is:

<?php
function array_to_params($in_arr) {
    $out_arr = array();

    foreach( $in_arr as $key => $value ) {
        $out_arr[] = new SoapParam($value, $key);
    }
    return $out_arr;
}

echo "<pre>";

$soapClient = new SoapClient(
    "http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL",
    array('trace' => true)
);

var_dump($soapClient->__getTypes());die();

$soap_user_params = array(
    'blank'         => 'blank', //if I don't have a dummy entry the first parameter doesn't show up.
    'AuthUserName'  => 'admin',
    'AuthPassword'  => 'derp'
);
$soap_user_params = array_to_params($soap_user_params);

$error = FALSE;
try {
    $info = $soapClient->__soapCall("GetAllDomains", $soap_user_params);
//  $info = $soapClient->GetAllDomains($soap_user_params); //same results
} catch (SoapFault $fault) {
    $error = TRUE;
    printf("Error %s: %s\n", $fault->faultcode, $fault->faultstring);
}

if( !$error ) {
    var_dump($info);
    echo "\nRequest: " . htmlentities($soapClient->__getLastRequest());
}

echo "</pre>";
?>

For reference, the WSDL for this function is:

<s:element name="GetAllDomains">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="AuthUserName" type="s:string"/>
            <s:element minOccurs="0" maxOccurs="1" name="AuthPassword" type="s:string"/>
        </s:sequence>
    </s:complexType>
</s:element>

The example request given is:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllDomains xmlns="http://tempuri.org/">
      <AuthUserName>string</AuthUserName>
      <AuthPassword>string</AuthPassword>
    </GetAllDomains>
  </soap:Body>
</soap:Envelope>

But the request generated by my code is:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:GetAllDomains/>
        <AuthUserName>admin</AuthUserName>
        <AuthPassword>derp</AuthPassword>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can see that the username and password parameters are not contained within the GetAllDomains section for some reason. As well, in the code you can see that I have to pass a dummy parameter in first because whatever is first does not show up in the request. Also, I cannot simply pass in an associative array because the keys are not used as parameter names, it puts in 'param1' and 'param2' instead.

I should note that the server accepts the request and spits back a message about the auth failing. There is a built-in SOAP request tester on the server, and when the request fits the given example it works normally.

Everything I've looked up in the PHP docs and through Google seems to indicate that it should not be this difficult. :(

Any help is GREATLY appreciated.

note: I am running PHP 5.3.8 on Apache 2.2 on FreeBSD, built and installed via ports today.

post-answer-edit:

Thanks to Gian's answer below I've gotten this working and greatly simplified the code:

<?php

echo "<pre>";

$soapClient = new SoapClient( "http://mailserver.net/Services/svcDomainAdmin.asmx?WSDL", array( 'trace' => true ) );

$soap_user_params = array(
    'AuthUserName'  => 'admin',
    'AuthPassword'  => 'derp'
);

try {
    $info = $soapClient->__soapCall("GetAllDomains", array( 'parameters' => $soap_user_params ) );
    var_dump($info);
    echo "\nRequest:\n" . htmlentities($soapClient->__getLastRequest());
} catch (SoapFault $fault) {
    printf("Error %s: %s\n", $fault->faultcode, $fault->faultstring);
}

echo "</pre>";

解决方案

My apologies for the mis-directed close. I misunderstood what you were trying to do at first.

Sifting through the soapCall documentation comments, there are a couple things I'd suggest trying. First off, this:

$soap_user_params = array(
    'parameters' => array (
        'AuthUserName'  => 'admin',
        'AuthPassword'  => 'derp'
    )
);

Apparently your parameters need to be an element in an associative array with the key 'parameters'. Weird, huh?

Failing that, you may need to nest this even deeper! The _ array entry does not appear to be documented anywhere obvious, but this seems to be something that the commenters on the PHP docs were suggesting. So maybe this:

$soap_user_params = array(
    'parameters' => array (
        'AuthUserName'  => array('_' => 'admin'),
        'AuthPassword'  => array('_' => 'derp')
    )
);

这篇关于PHP 5.3 SOAP请求是如何形成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆