Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send XML from URL on PHP submit

Let's imagine the following situation:

You have a simple php page with an html form that allows the user to enter the following data:

  • Phone Number
  • Text Message

Such as:

<form action="xml-file.xml" method="get">
   <input type="text" id="to" name="to" value="" />
   <input type="text" id="text" name="text" value="" />
   <input type="submit" />
</form>

Now, you create an XML using this method that I've tested and works without a problem, and afterwards you want to send it via URL using this other method (CURL).

Unfortunately, I can't reach the CURL method, as I can't link the submit action to both create the XML and send it to an URL so it goes to a local server (www.example.xxx:1234) and afterwards processes that XML for sending an SMS.

That server sends a response if the format of the XML is the right one. My issue is, therefore, on the submission of the created XML.

Help?

Update 1: Adding the updated code. This allows me (with a chmod 777 onto the sms.xml file) to edit the xml file at will.

index.php

<html>
...
<form action="send.php" method="get">
   <input type="text" id="to" name="to" value="" />
   <input type="text" id="text" name="text" value="" />
   <input type="submit">
</form>
...
</html>

send.php

<?php
  $xml = simplexml_load_file("sms.xml");          // Load XML file
  $xml->Title3 = $_GET['to'];                         // Updating <Title3> from GET method
  $xml->Title5[0]->Title51Content = $_GET['text'];      // Updating <Title51> from GET method
  $xml->asXML('sms.xml');                         // Saving the XML file
?>

sms.xml

<?xml version="1.0" encoding="UTF-8"?>
<Title1>
   <Title2>Some Text</Title2>
   <Title3>Variable 1</Title3>
   <Title4>Some Text</Title4>
   <Title5>
      <Title51>Variable 2</Title51>
   </Title5>
</Title1>

Side note: XML should have, when sending, the "Content-Type","application/x-www-form-urlencoded" header for returning something like this:

XML=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3...

Thanks!

like image 864
AleksanderKseniya Avatar asked Jul 16 '26 13:07

AleksanderKseniya


1 Answers

Return the XML as a string and post it?

...
$xml_post_string = 'XML='.urlencode($xml->asXML());  

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://theurl.com');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
curl_close($ch);
...
like image 106
Repox Avatar answered Jul 19 '26 02:07

Repox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!