PHP - cURL (post by XML)

<?php
//this url return result in xml format
$url = "http://www.exmple.com/fakeResponse.aspx";
$xml_request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
 <note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
 </note>";

$response = curl_xml($url, $xml_request);

/* ------- Response  -------*/
//header for response result in xml format
header('Content-type: text/xml');
echo $response;

/* ------- CURL_XML -------*/
function curl_xml($_url, $_xmlRequest) {
    $header = array("content-type: text/xml");
    // initialize curl handle
    $c = curl_init();

    // set url to post to
    curl_setopt($c, CURLOPT_URL, $_url);

    //include header as needed
    curl_setopt($c, CURLOPT_HTTPHEADER, $header);

    //not include the header in th output
    curl_setopt($c, CURLOPT_HEADER, 0);

    // set post method in request otherwise curl will do a GET request
    curl_setopt($c, CURLOPT_POST, 1);

    // add POST fields
    curl_setopt($c, CURLOPT_POSTFIELDS, $_xmlRequest);

    // return into a variable
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

    // times out after 8s
    curl_setopt($c, CURLOPT_TIMEOUT, 8);

    // run the whole process
    $xml_response = curl_exec($c);

    curl_close($c);

    return $xml_response;
}
/* ------------------------*/
?>

Popular posts from this blog

SAP CPI - Loop Process Call with SuccessFactor (oData V2)

Setting IntelliJ IDEA to run Groovy Script

C# - BASE64 to Image