I want to sent XML content(string) in the request body of the POST request. This string will not contain any parameter name.

On the server side, we are retrieving this text using

fopen("php://input", "r");


How can I sent this using cURL?

something like this maybe??

function curl_get_file_contents($URL)
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);

        if ($contents) return $contents;
            else return FALSE;

No, this is for GET request. I am asking to POST data to a URL.

ohh…my bad. Here’s a post:

$durl = "http://yourURLhere";

$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: ".$content_type));
    curl_setopt ($curl, CURLOPT_URL, $durl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);	
    curl_setopt($curl, CURLOPT_POST, TRUE);
    $result = curl_exec ($curl);
    curl_close ($curl);
    print $result;


[google]curl post php[/google]

How to post data(not query string or parameters, actually raw data, XML string) to a URL in POST request?

I want to send the string (XML) below in POST and PUT requests.

<?xml version="1.0">

How can I send it in a POST or PUT request?

Do it like this:

$myXML = "<xml>.....</xml>";  // content of your xml-file
$url = "h_t_t_p://";  // url where the data should be posted to
$headers=array("Accept: application/xml","Content-Type: application/xml");

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $myXML);
$myResponse = curl_exec ($curl);
curl_close ($curl);

I had to write the $url-stuff with “h_t_t_p” instead of “http” because this forum won’t allow me to post a link … :wink:
So you have to replace that one.


I am not sure without having a fieldname to assign the xml value like this

$vars = 'myxmldata=<xml>....</xml>';

will send the data to the specified page and how you are going to take the value in the target page… but if you give the fieldname like above, then it should go…


for more details.

The following example should work, I’ll give very basic examples of a client and server.



$xml     = '<request>Testing</request>';
$server  = '...'; // URL to server.php
$options = array
	CURLOPT_URL            => $server,
	CURLOPT_POST           => true,

$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);

echo '<pre>', htmlspecialchars($response), '</pre>';



$xml = file_get_contents('php://input');

$xml = new SimpleXMLElement($xml, LIBXML_NOCDATA, false);

$response = (string) $xml;
$response = strtoupper($response);

echo $response;

All things correct, the example client should simply output TESTING.