从cURL响应中提取数据不会返回任何值

时间:2014-06-23 10:35:22

标签: php xml json curl wsdl

我想要达到的目标:我正试图从网络服务器获得回应。有了这个响应,我有一些我需要提取的数据。我已经达到了接收网络服务响应的程度。

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

我尝试了什么: 我试图在数组中解析结果(没有返回值) 我试图用新的SimpleXMLElement(空对象)解析结果 我试着用JSON编码它再次解码它(空数组) 我试图爆炸结果(没有返回好的值)

我不知道该怎么办,有人可以看看。

我的cURL代码:

$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值。

2 个答案:

答案 0 :(得分:0)

尝试使用DOMDocument

$doc = new DOMDocument();
$doc->loadXML($output_cl);

$mpsid = $doc->getElementsByTagName( "mpsid" );
$mpsid_value= $mpsid->item(0)->nodeValue;

答案 1 :(得分:0)

这是一个注释代码示例。您基本上无法理解如何访问命名空间。我的例子显示了这个冗长的内容(我建议你自己构建它,在中间使用->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>"

如果您不想在代码中进行遍历,并且您已经了解了如何使用命名空间,那么这个旧的但非常有用的Q&amp; A。它介绍了XPath,它可以让你省去很多打字: