Php 从cURL响应中提取数据不返回任何值
我试图达到什么:我试图从一个网络服务器得到一个响应。有了这个回答,我有一些数据需要提取。我已经到了从Web服务收到响应的地步 问题是:我使用cURL从服务器得到了数据响应,但是我无法从答案中提取数据,因为它总是返回空值 我尝试的是: 我试图在数组中解析结果(没有返回值) 我试图用新的SimpleXMLElement(空对象)解析结果 我尝试用JSON对其进行编码,然后再次解码(空数组) 我试图分解结果(没有返回好的值) 我不知道该怎么办了,有人能看看吗 我的卷曲代码:Php 从cURL响应中提取数据不返回任何值,php,xml,json,curl,wsdl,Php,Xml,Json,Curl,Wsdl,我试图达到什么:我试图从一个网络服务器得到一个响应。有了这个回答,我有一些数据需要提取。我已经到了从Web服务收到响应的地步 问题是:我使用cURL从服务器得到了数据响应,但是我无法从答案中提取数据,因为它总是返回空值 我尝试的是: 我试图在数组中解析结果(没有返回值) 我试图用新的SimpleXMLElement(空对象)解析结果 我尝试用JSON对其进行编码,然后再次解码(空数组) 我试图分解结果(没有返回好的值) 我不知道该怎么办了,有人能看看吗 我的卷曲代码: $xml = '
$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;
}
}
$xml='1!'
“.$delisId。”
“.$auth_令牌。”
“.$messageLanguage。”
“.$printerLanguage”
“.$paperFormat。”
“.$identificationNumber。”
“.$sendingDepot”
“.$product。”
“.$MPS完成交付。”
“.$send_name”
“$send_street。”
“.$send_country”
“.$send_zipcode”
“.$send_city”
“.$send_customerNumber”
“.$rec_name”
“.$rec_街。”
“.$rec_state”
“.$rec_country”
“.$rec_zipcode”
“.$rec_city”
“.$parcelLabelNumber。”
“.$orderType。”
';
$headers=数组(
“发布HTTP/1.1”,
“内容类型:应用程序/soap+xml;字符集=\“utf-8\”,
“SOAPAction:\”http://dpd.com/common/service/ShipmentService/3.1/storeOrders\"",
“内容长度:”.strlen($xml)
);
$url='1https://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);
卷曲设置($cl,卷曲设置桩,1);
curl_setopt($cl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($cl,CURLOPT_POSTFIELDS,“$xml”);
curl_setopt($cl,CURLOPT_RETURNTRANSFER,1);
$output\u cl=curl\u exec($cl);
卷曲关闭($cl);
//返回$output\u cl;
$output\u xml=新的simplexmlement($output\u cl);
foreach($output\u xml->orderresult作为$orderresult)
{
foreach($orderresult->parcellabelspdf作为$pdf)
{
返回$pdf;
}
}
来自服务器的响应(我认为是字符串格式,但不确定):
pdflabel
身份证号码
mpsid
标签号
我想提取parcellabelspdf值和mpsid值。尝试使用
DOMDocument
$doc = new DOMDocument();
$doc->loadXML($output_cl);
$mpsid = $doc->getElementsByTagName( "mpsid" );
$mpsid_value= $mpsid->item(0)->nodeValue;
下面是一个注释代码示例。您在理解如何访问名称空间方面基本上存在问题。我的示例显示了这种冗长(我也建议您在自己构建时,使用
->asXML()
来调试您已经走了多远)
我假设代码的第二部分是从echo$output\u cl代码>。如果没有,请给出echo
@Athafoud的输出是的确实这是echo的输出为什么你仍然没有使用SoapClient?@Jack我一开始尝试使用SoapClient,但我遇到了一个主异常错误,没有进一步的错误细节。所以我试着用cURL来代替。但现在一切正常。谢谢大家的帮助!我只是尝试了您的解决方案,但仍然没有返回的值。因此,我认为您在解析信封时可能有错误。检查语法并尝试var_dump($doc)
查看字符串是否正确加载。如果在您编写的行之后使用var_dump($doc),我将得到以下响应:object(DOMDocument)#2(0){}。我检查了我所有的语法是否有错误,但我找不到。也许你能找到一个。var dump()的响应是否良好?var_dump()的结果是否良好。在我看来,你们的回复信封格式正确。我唯一能想到的是你有解析错误。您是否将php配置设置为显示所有错误和警告?是的,它显示了所有内容,我甚至尝试将响应放在字符串中,从webservice调用它,然后将其解析为对象。但这也会返回一个空对象。你可以发布一个例子,说明如何将字符串转换为对象,并引用其节点吗。。。使用SoapClient:P
$doc = new DOMDocument();
$doc->loadXML($output_cl);
$mpsid = $doc->getElementsByTagName( "mpsid" );
$mpsid_value= $mpsid->item(0)->nodeValue;
<?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>"