3

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!

1 Answer 1

1

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

1 Comment

Thanks for the answer. Would that $xml_post_string return the saved value from the asXML('sms.xml') that was saved previously? Also, does the CURLOPT_URL accepts a port number directly on the url? And, would that urlencode encode the resultant XML onto "Content-Type","application/x-www-form-urlencoded"?

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.