如何从PHP curl解码二进制响应

问题描述 投票:2回答:1
curl -vvvv -k -s -X POST --header 'Content-type: text/xml;charset="utf-8"' --header 'SOAPAction: vend' -u 'USER':'PIN' -d "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body><vend xmlns=\"http://host.vim.services.co.uk/xsd\"><sequence>6922343</sequence><origMsisdn>xxxxxxxxxxxxx </origMsisdn><destMsisdn>xxxxxxxxxxxxx</destMsisdn><amount>1000</amount><tariffTypeId>1</tariffTypeId></vend></soapenv:Body></soapenv:Envelope>" https://xxx.xxx.x.xxx:443/modlue/services/TargetOne ; echo;

上面的curl请求在下面(在终端中)得到我这个响应

    * Trying xxx.xxx.x.xxx...
    * TCP_NODELAY set
    * Connected to xxx.xxx.x.xxx (xxx.xxx.x.xxx) port 443 (#0)
    * ALPN, offering h2
    * ALPN, offering http/1.1
    * successfully set certificate verify locations:
    * CAfile: /etc/ssl/certs/ca-certificates.crt
    * CApath: /etc/ssl/certs
    * TLSv1.3 (OUT), TLS handshake, Client hello (1):
    * TLSv1.3 (IN), TLS handshake, Server hello (2):
    * TLSv1.2 (IN), TLS handshake, Certificate (11):
    * TLSv1.2 (IN), TLS handshake, Server key exchange (12):
    * TLSv1.2 (IN), TLS handshake, Server finished (14):
    * TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
    * TLSv1.2 (OUT), TLS change cipher, Client hello (1):
    * TLSv1.2 (OUT), TLS handshake, Finished (20):
    * TLSv1.2 (IN), TLS handshake, Finished (20):
    * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
    * ALPN, server did not agree to a protocol
    * Server certificate:
    * subject: CN=*.xxx.xxxxxxe.com
    * start date: Sep 10 00:00:00 2018 GMT
    * expire date: Nov 28 12:00:00 2020 GMT
    * issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=RapidSSL RSA CA 2018
    * SSL certificate verify ok.
    * Server auth using Basic with user 'username'
    > POST /axis2/services/HostIFService HTTP/1.1
    > Host: xxx.xxx.x.xxx
    > Authorization: Basic V4mp00vKL2wb3LL6Z2hZZSIzI2RzCL2kbNPlQjExMQ==
    > User-Agent: curl/7.58.0
    > Accept: */*
    > Content-type: text/xml;charset="utf-8"
    > SOAPAction: vend
    > Content-Length: 409
    >
    * upload completely sent off: 409 out of 409 bytes
    < HTTP/1.1 200
    < Content-Type: text/xml;charset=UTF-8
    < Content-Length: 568
    < Date: Wed, 15 Jan 2020 14:24:53 GMT
    < Vary: Accept-Encoding
    <
    * Connection #0 to host xxx.xxx.x.xxx left intact
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><xsd:vendResponse xmlns:xsd="http://xxxxxx.xxx.xxxxx.xx.xx/xxx"><xsd:sequence>0000000001</xsd:sequence><xsd:statusId>301</xsd:statusId><xsd:txRefId>2020011515245375604706230</xsd:txRefId><xsd:origBalance>0.00</xsd:origBalance><xsd:origMsisdn>12345678910112 </xsd:origMsisdn><xsd:destMsisdn>12345678910112</xsd:destMsisdn><xsd:responseCode>0</xsd:responseCode><xsd:responseMessage>Insufficient Airtime</xsd:responseMessage></xsd:vendResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

但是使用PHP curl只能输出

string(792) "00000000031060.002347064058107 23480320303031Sequence Number Check Failed2020012312555188502475422FAILED8076922343" with header informtion `"HTTP/1.1 200 Content-Type: text/xml;charset=UTF-8 Content-Length: 656 Date: Thu, 23 Jan 2020 12:20:02 GMT Vary: Accept-Encoding"` 

如何获取剩余内容并从xml响应中读取相应的值?

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><xsd:vendResponse xmlns:xsd="http://xxxxxx.xxx.xxxxx.xx.xx/xxx"><xsd:sequence>0000000001</xsd:sequence><xsd:statusId>301</xsd:statusId><xsd:txRefId>2020011515245375604706230</xsd:txRefId><xsd:origBalance>0.00</xsd:origBalance><xsd:origMsisdn>12345678910112 </xsd:origMsisdn><xsd:destMsisdn>12345678910112</xsd:destMsisdn><xsd:responseCode>0</xsd:responseCode><xsd:responseMessage>Insufficient Airtime</xsd:responseMessage></xsd:vendResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

编辑这是我用来发送请求的PHP代码。但是我希望能够以XML格式获取响应,然后提取txrefid之类的XML内容以及响应和消息。

$request = "<xml>Request</xml>";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://xxx.xxx.xxx.xxx:443/request");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-type: text/xml',
        'charset: utf-8',
        'action: action',
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_ENCODING,'');
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);

    $server_output = curl_exec($ch);

    $error    = curl_error($ch);
    $errno    = curl_errno($ch);
    if (0 !== $errno) {
        throw new \RuntimeException($error, $errno);
    }
    else{
        var_dump($server_output);
    }
    curl_close ($ch);
php xml xml-parsing xmlhttprequest php-curl
1个回答
0
投票

尝试从终端发送在curl中使用的相同标题:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-type: text/xml;charset="utf-8"',
    'SOAPAction: vend'
));
© www.soinside.com 2019 - 2024. All rights reserved.