Paulo Coghi
Paulo Coghi

Reputation: 14949

PHP: How to send REST requests with XML data

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!

Upvotes: 2

Views: 5715

Answers (2)

Paulo Coghi
Paulo Coghi

Reputation: 14949

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);

Upvotes: 4

Jason McCreary
Jason McCreary

Reputation: 72961

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.

Upvotes: 0

Related Questions