Answer the question
In order to leave comments, you need to log in
Sending a curl post request with data in xml?
There is some XML and a link to a third-party service:
$url = 'https://example.com/';
$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<Request>
<Language>ru</Language>
</Request>';
<form method="post" action="https://example.com/">
<textarea name="Request"><?php echo $xml; ?></textarea>
<button>Submit</button>
</form>
function sendXmlOverPost($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
print_r(sendXmlOverPost($url, $xml));
Answer the question
In order to leave comments, you need to log in
The problem is that from the form you send data as Request=XXXXXXXXX
because textarea name="Request", and in curl you send data in the form of raw post data.
The correct submission option, similar to how the data was sent from the form below
Content-Type: text/xml, which FanatPHP clung to, does not play a role, and it can not be sent.
function sendXmlOverPost($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['Request' => $xml]));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
print_r(sendXmlOverPost($url, $xml));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question