使用php和curl通过http发布xml

时间:2017-05-16 04:12:51

标签: php xml http curl post

我一直在关注我在此处找到的其他一些示例,包括以下示例:Send an XML post request to a web server with CURL以及一些外部示例,例如http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/

由于某些原因,我的代码无效。在我尝试访问实际服务器上的php文件很长一段时间后,我反复得到0的响应。

当我将测试结果发布到Chrome Advance REST客户端中的同一请求网址时,完全相同的XML会返回200 OK的响应,因此网址显然正常。

有谁能告诉我我的PHP代码有什么问题?

<?php 
$xml_data ='<cXML version="1.2.005" xml:lang="en-US" payloadID="removedforprivacy" timestamp="2017-05-15T13:00:00.000">'
   .'<Header>'
      .'<From>'
          .'<Credential domain="DUNS">'
              .'<Identity>removedforprivacy</Identity>'
          .'</Credential>'
          .'<Credential domain="CompanyName">'
              .'<Identity>removedforprivacy</Identity>'
          .'</Credential>'
      .'</From>'
      .'<To>'
          .'<Credential domain="CompanyName">'
              .'<Identity>removedforprivacy</Identity>'
          .'</Credential>'
      .'</To>'
      .'<Sender>'
          .'<Credential domain="DUNS">'
              .'<Identity>removedforprivacy</Identity>'
              .'<SharedSecret>removedforprivacy</SharedSecret>'
          .'</Credential>'
      .'</Sender>'
   .'</Header>'
   .'<Request deploymentMode="production">'
      .'<OrderRequest>'
          .'<OrderRequestHeader orderID="999" orderDate="2017-05-08 02:41:17" type="new">'
              .'<BillTo>'
                  .'<Address>'
                      .'<Name xml:lang="en-US">Nicole Testing</Name>'
                      .'<PostalAddress name="Nicole Testing">'
                          .'<DeliverTo>Nicole Testing</DeliverTo>'
                          .'<Street>1 Test St.</Street>'
                          .'<Street></Street>'
                          .'<City>Melbourne</City>'
                          .'<State>VIC</State>'
                          .'<PostalCode>3000</PostalCode>'
                          .'<Country isoCountryCode="AU">AU</Country>'
                      .'</PostalAddress>'
                  .'</Address>'
              .'</BillTo>'
              .'<Comments xml:lang="en-US"></Comments>'
          .'</OrderRequestHeader>'
          .'<ItemOut lineNumber="1" quantity="1" requestedDeliveryDate="2017-05-20T13:49:32">'
              .'<ItemID>'
                  .'<SupplierPartID>51239929_GC</SupplierPartID>'
                  .'<SupplierPartAuxiliaryID>Joe Simth</SupplierPartAuxiliaryID>'
              .'</ItemID>'
              .'<ItemDetail>'
                  .'<UnitPrice>'
                      .'<Money currency="AUD">1.32</Money>'
                  .'</UnitPrice>'
                  .'<Description xml:lang="en-US">Relaxed Tiger</Description>'
                  .'<UnitOfMeasure>Landscape Size: 24” x 16”</UnitOfMeasure>'
                  .'<URL>http://image.url.removed.for.privacy</URL>'
                  .'<Extrinsic name="quantityMultiplier">1</Extrinsic>'
              .'</ItemDetail>'
              .'<ShipTo>'
                  .'<Address addressID="0001">'
                      .'<Name xml:lang="en-US">Your Name</Name>'
                      .'<PostalAddress name="">'
                          .'<DeliverTo>Nicole Testing</DeliverTo>'
                          .'<Street>1 Test St.</Street>'
                          .'<Street></Street>'
                          .'<City>Melbourne</City>'
                          .'<State>VIC</State>'
                          .'<PostalCode>3000</PostalCode>'
                          .'<Country isoCountryCode="AU">AU</Country>'
                      .'</PostalAddress>'
                  .'</Address>'
              .'</ShipTo>'
          .'</ItemOut>'
      .'</OrderRequest>'
   .'</Request>'
.'</cXML>';

$url = 'http://removed.for.privacy';

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); // also tried this with application/xml and same response.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml_data );
$response = curl_exec($ch);
print_r($response);
echo "HTTP response code: ".(int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

?>

2 个答案:

答案 0 :(得分:0)

我的代码似乎没有任何问题,而是端口:8080被防火墙阻止了。请求URL使用端口:8080,因此返回状态为0。

答案 1 :(得分:-1)

我不熟悉传输XML,但使用heredocs 可取,在你的情况下它将是:

$xml_data = <<<EOD
<cXML version="1.2.005" xml:lang="en-US" payloadID....
   //xml body here
</cXML>
EOD;