Solved

PHP - create XML file?

Posted on 2014-03-10
5
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39917157
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 110

Expert Comment

by:Ray Paseur
ID: 39917188
Also, it looks like Postcode != PostCode (case-sensitive tags).
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39917192
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
ID: 39917230
Thanks Ray, that is fantastic.  very much appreciated.. Cheers.  Andrew
0
 
LVL 110

Expert Comment

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

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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