Kurinchi Blogger Scribbles …


Posts Tagged ‘XML data’

Oct 05
2009

PHP: How to receive the posted XML data?

Last updated: October 5th, 2009

Receive the posted XML data

In order to test the posted data, we can create a file creation steps to ensure that we receive the posted data via the $_POST array

<?php
/*
  File name: postdata2.php
*/
    $xmlFile = "xmlFile.txt";
    $fh = fopen($xmlFile, 'w') or die("Cannot open file");
    fwrite($fh, $_POST["xmldata"]);
    fclose($fh);

?>
Oct 04
2009

PHP: How to post XML data using CURL?

Last updated: October 5th, 2009

During integration projects or during such similar situations you may have across a necessity to transfer XML data or plain text file across server locations.

Curl comes in handy during such scenarios. Following program is used to post XML data using Curl. In order for Curl to function, ensure that PHP settings has the curl module installed and that the libraries/dll libeay32, ssleay32 is installed in your server.

<?php
/*
  File name: postdata.php
*/

$post_url = "http://localhost/demo/postdata2.php";
$xml_string = '<?xml version="1.0" encoding="UTF-8"?>
<books>
<book isbn="978-1594489587">
    <title>The Brief Wondrous Life of Oscar Wao</title>
    <publisher>Riverhead Hardcover </publisher>
    <amazon_price>14.97 </amazon_price>
    <author_firstname>Junot </author_firstname>
    <author_lastname>Diaz </author_lastname>
  </book>
<book isbn="999-1594489501">
    <title>A Thousand Splendid Suns </title>
    <publisher>Riverhead Hardcover </publisher>
    <amazon_price>14.27 </amazon_price>
    <author_firstname>Khaled </author_firstname>
    <author_lastname>Hosseini </author_lastname>
  </book>
</books>';

$ch = curl_init($post_url);

curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "xmldata=".$xml_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

$data = curl_exec($ch);
// to get information on the curl resultset
$info = curl_getinfo($ch);

if(curl_errno($ch)){
    print curl_error($ch);
}

curl_close($ch);

?>

Valid HTML 4.01 Strict  Valid HTML 4.01 Strict