** SOAP错误:**编码:找不到编码

时间:2018-11-14 20:56:31

标签: php soap soap-client

我必须使用PHP的SOAP服务,但始终无法获得响应。也许是格式,通话或IDK的问题。例如,我可以使用 __ getFunctions()方法从服务器获取所有函数名称。但是当我尝试调用任何函数时,我总是得到:

  

SOAP-ERROR:编码:找不到编码

下面是代码。

$wsdl = "https://testing.memoryefactura.com/Memory.FEManager/WebService/CFEService.svc?wsdl";


$parameters = array('Rut' => 'XXXXX',
    'CommerceCode' => 'XXXXX',
    'TerminalCode' => 'XXXXX',
    'Timeout' => 5000);


$options = array(
    'style' => SOAP_RPC,
    'use' => SOAP_ENCODED,
    'soap_version' => SOAP_1_1,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'connection_timeout' => 15,
    'trace' => true,
    'exceptions' => true,
);
try {
    $soap = new SoapClient($wsdl, $options);
    $HeaderSecurity = array(
        'stream_context' => stream_context_create(array(
            'http' => array(
                'header' => array('username' => 'XXXXX',
                    'password' => 'XXXXX'
                )
            ),
        )),
    );
    $header[] = new SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", $HeaderSecurity);

    $soap->__setSoapHeaders($header);
    $data = $soap->Ping($parameters);
} catch (Exception $e) {
    die($e->getMessage());
}

var_dump($data);

1 个答案:

答案 0 :(得分:1)

基于wsdl,ping方法仅需要3个参数。

class Ping {
    /** @var BaseMessage */ public $message;
}
class BaseMessage {
    /** @var string */  public $CommerceCode;
    /** @var string */  public $TerminalCode;
    /** @var int */ public $Timeout;
}

此外,您错误地设置了授权标头。正确的方法:

$wsdl = "https://testing.memoryefactura.com/Memory.FEManager/WebService/CFEService.svc?wsdl";

$opts = [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ],
    'http' => [
        'user_agent' => 'PHPSoapClient'
    ]
];

$params = [
    'encoding' => 'UTF-8',
    'verifypeer' => false,
    'verifyhost' => false,
    'soap_version' => SOAP_1_1,
    'trace' => 1,
    'exceptions' => 1,
    'connection_timeout' => 180,
    'stream_context' => stream_context_create($opts)
];

try {
    $client = new \SoapClient($wsdl, $params);

    $nameSpace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    $soapUsername = new \SoapVar(
        'XXXXX',
        XSD_STRING,
        null,
        $nameSpace,
        null,
        $nameSpace
    );

    $soapPassword = new \SoapVar(
        'XXXXX',
        XSD_STRING,
        null,
        $nameSpace,
        null,
        $nameSpace
    );

    $auth = new \stdClass();
    $auth->Username = $soapUsername;
    $auth->Password = $soapPassword;

    $soapAuth = new \SoapVar(
        $auth,
        SOAP_ENC_OBJECT,
        null,
        $nameSpace,
        'UsernameToken',
        $nameSpace
    );

    $token = new \stdClass();
    $token->UsernameToken = $soapAuth;

    $soapToken = new \SoapVar(
        $token,
        SOAP_ENC_OBJECT,
        null,
        $nameSpace,
        'UsernameToken',
        $nameSpace
    );

    $security = new \SoapVar(
        $soapToken,
        SOAP_ENC_OBJECT,
        null,
        $nameSpace,
        'Security',
        $nameSpace
    );

    $header = new \SoapHeader($nameSpace, 'Security', $security, true);

    $client->__setSoapHeaders([$header]);

    $parameters = array(
        'CommerceCode' => 'XXXXX',
        'TerminalCode' => 'XXXXX',
        'Timeout' => 5000
    );

    $data = $client->Ping($parameters);
} catch (SoapFault $fault) {
    echo "REQUEST:\n" . $client->__getLastRequest();
    die("\nFaultcode: " . $fault->faultcode . "\nFaultstring: " . $fault->faultstring);
} catch (Exception $e) {
    die($e->getMessage());
}