-1

I have been following some other examples I found on here including this one: Send an XML post request to a web server with CURL and also some external examples such as http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/

For some reason my code is not working. I am repeatedly getting a response of 0 after a lengthy period of time when I try visiting the php file on the actual server.

The exact same XML returns a response of 200 OK when I test posting it to the same request URL in the Chrome Advance REST Client, so clearly the URL is working.

Can anyone please tell me what is wrong with my PHP code?

<?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);

?>
Community
  • 1
  • 1
nicole2292
  • 111
  • 1
  • 9
  • 1
    Reduce this code to a [mcve]. – Arya McCarthy May 16 '17 at 05:45
  • "I am repeatedly getting a response of 0 after a lengthy period of time when I try visiting the php file on the actual server." — Since we know nothing about the code on that server, we can't tell what is wrong with the request. – Quentin May 19 '17 at 09:41

2 Answers2

0

It would appear that there is nothing wrong with my code and rather port :8080 was blocked by a firewall. The request URL was using port :8080 so it was returning a status of 0.

nicole2292
  • 111
  • 1
  • 9
-1

I'm not familiar with transferring XML, but it's desirable to use heredocs, in your case it will be:

$xml_data = <<<EOD
<cXML version="1.2.005" xml:lang="en-US" payloadID....
   //xml body here
</cXML>
EOD;
D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41
  • Thank you for your reponse. Heredoc was not mentioned in either of the examples I was referencing specific to XML transfer. Can you please direct me to an example of their usage in this type of case? – nicole2292 May 16 '17 at 08:09
  • FYI: You will find more info about heredoc [here](https://php.net/manual/language.types.string.php#language.types.string.syntax.heredoc). It will indeed improve code legibility but comes with a performance penalty. If you have no dynamic parts in your XML, consider keeping a separate file. – DaSourcerer May 16 '17 at 09:08
  • "but you for sure have to use heredocs" — While it might make the code easier to read, there is absolutely no requirement to use it here. – Quentin May 19 '17 at 09:39