How to http post raw data in php

This example shows how to post raw data over http in php. For exemple if you want to send an XML-document to a web service.

$xmlData = ‘…’;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ‘http://example.com/service.php’);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(“Content-Type: application/xml”));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($curl);
curl_close ($curl);

The variable $response now contains the server response.

Leave a Reply

Your email address will not be published. Required fields are marked *