Solved

PHP - create XML file?

Posted on 2014-03-10
5
646 Views
Last Modified: 2014-03-10
Hi,
I am trying to create an XML file and then post to interparcel but not doing anything, not sure what I am doing wrong?

Your feedback would be appreciated.

<?php
// Your post data
//$post_city= $_POST['city'];
//$post_postcode = $_POST['postcode'];

$post_city= 'Melbourne';
$post_postcode = '3001';



// Building your XML string
$strXML = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$strXML .= '<Request>'."\n";
$strXML .= '<Authentication>'."\n";
$strXML .= '<UserID>admin@onlinestore.com.au</UserID>'."\n";
$strXML .= '<Password>onlinepass</Password>'."\n";
$strXML .= '<Version>1.0</Version>'."\n";
$strXML .= '</Authentication>'."\n";

$strXML .= '<RequestType>Rates</RequestType>'."\n";
$strXML .= '<ShowAvailability>N</ShowAvailability>'."\n";

$strXML .= '<Shipment>'."\n";

$strXML .= '<Collection>'."\n";

$strXML .= '<City>Niddrie</City>'."\n";
$strXML .= '<Country>Australia</Country>'."\n";
$strXML .= '<Postcode>3042</Postcode>'."\n";
$strXML .= '</Collection>'."\n";

$strXML .= '<Delivery>'."\n";
$strXML .= '<City>'.$post_city.'</City>'."\n";
$strXML .= '<Country>Australia</Country>'."\n";
$strXML .= '<Postcode>'.$post_postcode.'</Postcode>'."\n";
$strXML .= '</Delivery>'."\n";

$strXML .= '<Package>'."\n";
$strXML .= '<Weight>4</Weight>'."\n";
$strXML .= '<Length>43</Length>'."\n";
$strXML .= '<Width>46</Width>'."\n";
$strXML .= '<Height>66</Height>'."\n";
$strXML .= '</Package>'."\n";

$strXML .= '</Shipment>'."\n"; 
$strXML .= '</Request>'."\n";


$url = "https://www.interparcel.com.au/api/xml/rates.php";

$post_string = $strXML;


$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n"; 
$header .= $post_string;

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

$data = curl_exec($ch); 

if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);


?>

Open in new window

0
Comment
Question by:sabecs
  • 4
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You might find this easier with HEREDOC notation.  Also, I believe that you must tell cURL that you're using a POST-method request.  This is my teaching example. YMMV, but it appears to work most of the time and it can give you error diagnostic information if it fails.

function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Also, it looks like Postcode != PostCode (case-sensitive tags).
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Give this a try.  It will at least get you a meaningful response!
http://www.iconoun.com/demo/temp_sabecs.php

<?php // demo/temp_sabecs.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28384341.html


function curl_post($url, $post_string=NULL, $timeout=2, $error_report=TRUE)
{
    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE          );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// THESE MUST BE SET BEFORE THE XML DOCUMENT IS CREATED
$post_city= 'Melbourne';
$post_postcode = '3001';



// CREATE THE XML DOCUMENT USING HEREDOC NOTATION
$xml = <<<ENDXML
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Authentication>
  <UserID>admin@onlinestore.com.au</UserID>
  <Password>onlinepass</Password>
  <Version>1.0</Version>
</Authentication>
<RequestType>Rates</RequestType>
<ShowAvailability>N</ShowAvailability>
<Shipment>
  <Collection>
    <City>Niddrie</City>
    <Country>Australia</Country>
    <PostCode>3042</PostCode>
  </Collection>
  <Delivery>
    <City>$post_city</City>
    <Country>Australia</Country>
    <PostCode>$post_postcode</PostCode>
  </Delivery>
  <Package>
    <Weight>4</Weight>
    <Length>43</Length>
    <Width>46</Width>
    <Height>66</Height>
  </Package>
</Shipment>
</Request>
ENDXML;

// TEST TO SEE IF THE XML STRING IS VALID
$obj = SimpleXML_Load_String($xml);

// SET THE URL AND TRY THE POST REQUEST
$url = "https://www.interparcel.com.au/api/xml/rates.php";
$res = curl_post($url, $xml, 6);
var_dump($res);

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:sabecs
Comment Utility
Thanks Ray, that is fantastic.  very much appreciated.. Cheers.  Andrew
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Glad to help!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now