Skip to content

Instantly share code, notes, and snippets.

@anik
Created May 20, 2019 17:45
Show Gist options
  • Save anik/887a647d7518242770e29bce932c60d6 to your computer and use it in GitHub Desktop.
Save anik/887a647d7518242770e29bce932c60d6 to your computer and use it in GitHub Desktop.
<?php
echo 'PHP POST Request <br/>';
function curl_data($data) {
// make sure curl module is enable
// phpinfo();
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/xml_action/index.php'); // Response URL
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml") );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if( $response === false ) throw new Exception(curl_error($ch), curl_errno($ch));
curl_close($ch);
} catch (Exception $e) {
$response = 'Curl failed with error ' . $e->getCode() . ' ' . $e->getMessage();
}
return $response;
}
$xml = file_get_contents('send_data.xml'); // for file_get_contents make sure that php.ini necessary module is enable
$return_data = curl_data( $xml );
$obj_info = simplexml_load_string($return_data);
echo '<pre>';
print_r( $obj_info );
echo '</pre>';
// Save XML
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$root = $dom->createElement('information');
foreach( $obj_info as $key => $value ){
$child_node = $dom->createElement($key, $value );
$root->appendChild($child_node);
}
$dom->appendChild($root);
$dom->save('save_info.xml');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment