2

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!

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Ignore the line $auth."\r\n" I'm using it for Basic Authentication, but it's not necessary for other purposes.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.