views:

38

answers:

2

Hi!

I need to make a request to an API, using REST (POST method) in PHP.

But the data needs to be in XML format. How can I send REST requests with XML data?

Thank you!

A: 

curl

This can be used to finely set several headers - POST, PUT, DELETE - for you REST request as well as send a payload - your XML content.

Jason McCreary
A: 

I used "fopen" and it works.

//You can use 'POST','GET','PUT' or 'DELETE'
$opts = array(
    'http'=>array(
        'method'=>'POST',
        'header'=>"Content-Type: text/xml\r\n" .
            $auth."\r\n",
        'content'=>$xml
    )
);

$context = stream_context_create($opts);

/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen($url, 'r', false, $context);
fpassthru($fp);
fclose($fp);
Paulocoghi
Ignore the line $auth."\r\n"I'm using it for Basic Authentication, but it's not necessary for other purposes.
Paulocoghi