Solved

PHP - create XML file?

Posted on 2014-03-10
5
649 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 109

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 109

Expert Comment

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

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 109

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySql and Apache/PHP Error Reporting 2 28
error log using ftp 7 41
JSON  parse help 7 27
How to check if a session is disconnected in php 9 17
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 …

825 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