从cURL响应中提取数据一直没有返回值


Extract data from cURL response keeps returning no value

我想要达到什么:我试图从web服务器获得响应。有了这个响应,我有一些数据,我需要提取。我已经到达了从webservice接收响应的点。

问题是什么:我使用cURL从服务器获得数据响应,但我无法从答案中提取数据,因为它总是返回空值。

我试过的:我试图在数组中解析结果(没有返回值)我试图用新的SimpleXMLElement(空对象)解析结果我试着用JSON编码,然后再解码(空数组)我试图爆炸结果(没有返回好的值)

我不知道该怎么办了,谁来帮我看看。

My cURL code:

$xml = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://dpd.com/common/service/types/Authentication/2.0" xmlns:ns1="http://dpd.com/common/service/types/ShipmentService/3.1"> 
    <soapenv:Header> 
        <ns:authentication> 
            <delisId>'.$delisId.'</delisId> 
            <authToken>'.$auth_token.'</authToken> 
            <messageLanguage>'.$messageLanguage.'</messageLanguage> 
        </ns:authentication> 
    </soapenv:Header> 
    <soapenv:Body> 
        <ns1:storeOrders> 
            <printOptions> 
                <printerLanguage>'.$printerLanguage.'</printerLanguage> 
                <paperFormat>'.$paperFormat.'</paperFormat> 
            </printOptions> 
            <order> 
                <generalShipmentData> 
                    <identificationNumber>'.$identificationNumber.'</identificationNumber> 
                    <sendingDepot>'.$sendingDepot.'</sendingDepot> 
                    <product>'.$product.'</product> 
                    <mpsCompleteDelivery>'.$mpsCompleteDelivery.'</mpsCompleteDelivery> 
                    <sender> 
                        <name1>'.$send_name.'</name1> 
                        <street>'.$send_street.'</street> 
                        <country>'.$send_country.'</country> 
                        <zipCode>'.$send_zipcode.'</zipCode> 
                        <city>'.$send_city.'</city> 
                        <customerNumber>'.$send_customerNumber.'</customerNumber> 
                    </sender> 
                    <recipient> 
                        <name1>'.$rec_name.'</name1> 
                        <street>'.$rec_street.'</street> 
                        <state>'.$rec_state.'</state> 
                        <country>'.$rec_country.'</country> 
                        <zipCode>'.$rec_zipcode.'</zipCode> 
                        <city>'.$rec_city.'</city> 
                    </recipient> 
                </generalShipmentData> 
                <parcels> 
                    <parcelLabelNumber>'.$parcelLabelNumber.'</parcelLabelNumber> 
                </parcels> 
                <productAndServiceData> 
                    <orderType>'.$orderType.'</orderType> 
                </productAndServiceData> 
            </order> 
        </ns1:storeOrders> 
    </soapenv:Body> 
</soapenv:Envelope>
    ';
    $headers = array(
        "POST  HTTP/1.1",
        "Content-type: application/soap+xml; charset='"utf-8'"",
        "SOAPAction: '"http://dpd.com/common/service/ShipmentService/3.1/storeOrders'"",
        "Content-length: ".strlen($xml)
    );
    $url = 'https://public-ws-stage.dpd.com/services/ShipmentService/V3_1/';
    $cl = curl_init();
    curl_setopt($cl, CURLOPT_URL, $url);
    curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($cl, CURLOPT_POST, 1);
    curl_setopt($cl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($cl, CURLOPT_POSTFIELDS, "$xml");
    curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
    $output_cl = curl_exec($cl);
    curl_close($cl);
    //return $output_cl;

    $output_xml = new SimpleXMLElement($output_cl);
    foreach($output_xml->orderresult as $orderresult)
    {
        foreach($orderresult->parcellabelspdf as $pdf)
        {
            return $pdf;
        }
    }

来自服务器的响应(我认为是字符串格式,但不确定):

<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:body>
        <ns2:storeordersresponse xmlns:ns2="http://dpd.com/common/service/types/ShipmentService/3.1">
            <orderresult>
                <parcellabelspdf>pdflabel</parcellabelspdf>
                <shipmentresponses>
                    <identificationnumber>idnumber</identificationnumber>
                    <mpsid>mpsid</mpsid>
                    <parcelinformation>
                        <parcellabelnumber>labelnumber</parcellabelnumber>
                    </parcelinformation>
                </shipmentresponses>
            </orderresult>
        </ns2:storeordersresponse>
    </soap:body>
</soap:envelope>

我想提取parcellabelspdf值和mpsid值

尝试使用DOMDocument

$doc = new DOMDocument();
$doc->loadXML($output_cl);
$mpsid = $doc->getElementsByTagName( "mpsid" );
$mpsid_value= $mpsid->item(0)->nodeValue;

下面是一个带注释的代码示例。在理解如何访问名称空间方面存在一些基本问题。我的示例显示了这些冗长的内容(我还建议您在自己构建时,在中间使用->asXML()来调试您已经完成了多少)。

<?php
/*
 * @link https://stackoverflow.com/questions/24363786/extract-data-from-curl-response-keeps-returning-no-value
 */
$buffer = <<<XML
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:body>
        <ns2:storeordersresponse xmlns:ns2="http://dpd.com/common/service/types/ShipmentService/3.1">
            <orderresult>
                <parcellabelspdf>pdflabel</parcellabelspdf>
                <shipmentresponses>
                    <identificationnumber>idnumber</identificationnumber>
                    <mpsid>mpsid</mpsid>
                    <parcelinformation>
                        <parcellabelnumber>labelnumber</parcellabelnumber>
                    </parcelinformation>
                </shipmentresponses>
            </orderresult>
        </ns2:storeordersresponse>
    </soap:body>
</soap:envelope>
XML;

$xml = simplexml_load_string($buffer);
// into <soap:*> namespace
$body        = $xml->children('soap', true)->body;
// into <ns2:*> namespace
$storeordersresponse = $body->children('ns2', true)->storeordersresponse;
// into document default namespace (no namespace)
$orderresult = $storeordersresponse->children()->orderresult; 
// you can now traverse as usual as you've reached the <orderresult> element
$mpsid       = $orderresult->shipmentresponses->mpsid;
echo $mpsid->asXML(); // prints "<mpsid>mpsid</mpsid>"

如果您不想在代码中进行遍历,并且您已经了解了如何使用名称空间,那么这里有一个古老但非常有用的问题A。它介绍了XPath的提示,可以省去大量的输入:

  • 用SimpleXML解析具有多个名称空间的XML