XML Request using PHP?

This XML request is to make a trial account for one of my websites. I don't really understand XML at all. So I want to have users be able to register from my page for a free 30 day account I understand the variables I need to put into the request below that must be there and I understand what variable I want edited to change features and the such.

But I would like to have the user fill the form out on my site to make the account using PHP. How do I write the file that takes the form data and sends it to the xml path. with all the information required below?

I have read through the knowledge base but I still don't quite understand.

<xmlrequest>
<username>[USERNAME]</username>
<usertoken>[TOKEN]</usertoken>
<requesttype>user</requesttype>
<requestmethod>createnewuser</requestmethod>
<details>
<trialuser>1</trialuser>
<username>test_trial_user</username>
<password>password</password>
<fullname>Trial User</fullname>
<emailaddress>trial_user@somedomain.com</emailaddress>
<usertimezone>GMT+10</usertimezone>
<permissions>
<autoresponders>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<approve>1</approve>
</autoresponders>
<forms>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
</forms>
<newsletters>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<approve>1</approve>
<send>1</send>
</newsletters>
<templates>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<approve>1</approve>
<import>1</import>
<global>1</global>
<builtin>1</builtin>
</templates>
<subscribers>
<manage>1</manage>
<add>1</add>
<edit>1</edit>
<delete>1</delete>
<import>1</import>
<export>1</export>
<banned>1</banned>
<eventsave>1</eventsave>
<eventdelete>1</eventdelete>
<eventupdate>1</eventupdate>
</subscribers>
<lists>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<bounce>1</bounce>
<bouncesettings>1</bouncesettings>
</lists>
<customfields>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
</customfields>
<statistics>
<newsletter>1</newsletter>
<user>1</user>
<autoresponder>1</autoresponder>
<list>1</list>
<triggeremails>1</triggeremails>
</statistics>
<user>
<smtp>1</smtp>
<smtpcom>1</smtpcom>
</user>
<segments>
<view>1</view>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<send>1</send>
</segments>
<triggeremails>
<create>1</create>
<edit>1</edit>
<delete>1</delete>
<activate>1</activate>
</triggeremails>
</permissions>
</details>
</xmlrequest>
LVL 2
KrzyviewAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I can probably help you get started with this.  Your form data will arrive in a POST array.  You copy the POST fields into the XML string.  If you start with a pre-written XML string such as that shown in the code snippet you can use the SimpleXML class to manipulate the XML object then you can write it out with the AsXML() method.
<?php // RAY_temp_krzyview.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// AN XML STRING IS EASIER TO READ IF IT IS SPACED AND INDENTED
$xml = <<<EOXML
<xmlrequest>
  <username>[USERNAME]</username>
  <usertoken>[TOKEN]</usertoken>
  <requesttype>user</requesttype>
  <requestmethod>createnewuser</requestmethod>

  <details>
    <trialuser>1</trialuser>
    <username>test_trial_user</username>
    <password>password</password>
    <fullname>Trial User</fullname>
    <emailaddress>trial_user@somedomain.com</emailaddress>
    <usertimezone>GMT+10</usertimezone>

    <permissions>
      <autoresponders>
        <create>1</create>
        <edit>1</edit>
        <delete>1</delete>
        <approve>1</approve>
      </autoresponders>

      <forms>
        <create>1</create>
        <edit>1</edit>
        <delete>1</delete>
      </forms>
    </permissions>

  </details>
</xmlrequest>
EOXML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// COPY AND CHANGE ELEMENTS IN THE OBJECT
$new = $obj;
$new->details->username = 'Ray';
unset($new->details->permissions->autoresponders);
$new->details->permissions->forms->create = '0';

// ACTIVATE THIS TO VISUALIZE THE MODIFIED OBJECT
// var_dump($new);

// CREATE THE NEW XML
$str = $new->AsXML();
echo htmlentities($str);

Open in new window

KrzyviewAuthor Commented:
I'm horrible i look at this at the moment and it's garbled to me im just so confused. I would create a form with the specific values and have it post to this php file to process the xml correct?
Ray PaseurCommented:
I don't know what you mean by "process the XML" - can you describe the application you're trying to write and tell us a little about your depth of experience in PHP?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

KrzyviewAuthor Commented:
My experience in PHP is at a point where I know how to perform changes, edits and addon's to just about any application written in php. But I do not understand the construction of it in general. I have a problem understanding arrays and the such unless I have some kind of example to work from.

What I am trying to do is allow a user to register on my site without me having to make the account in the back end from a Registration form on a Register.html page. I would like the form to take user name, address, phone number and email and submit this to the api url where it would actually automate the process of the registration.

When I say process the XML i mean how do i actually make the form send the request, to actually begin processing the request server side by the application
Ray PaseurCommented:
Would I be on firm ground if I paraphrased this to say, "I want to collect information in an HTML form and use PHP to create an XML string from the data?"
KrzyviewAuthor Commented:
Yes sir you would be correct. I know I may have made it more complicated then necessary.
Ray PaseurCommented:
OK, I can provide a simple example.  You will just need to extrapolate this to cover all your data points.
<?php // RAY_temp_krzyview.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// EXPECTED DATA - YOU MIGHT WANT TO ADD SOME SANITY CHECKS ON THE INPUT
$expected = array ( 'username', 'usertoken', 'requesttype', );

// HAS ANYTHING BEEN POSTED TO THIS SCRIPT
if (!empty($_POST))
{
    // USE THE POSTED KEYS AND DATA TO CREATE XML STRING
    $xml = '<?xml version="1.0"?>' . PHP_EOL . '<xmlrequest>' . PHP_EOL;
    foreach ($_POST as $key => $data)
    {
        // ONLY USE THE EXPECTED INPUT FIELDS
        if (in_array($key, $expected))
        {
            // CREATE AN XML TAG AND VALUE
            $xml .= "<$key>" . $data . "</$key>" . PHP_EOL;
        }
    }

    /// END OF CREATING XML - CLOSE THE XML STRING
    $xml .= '</xmlrequest>' . PHP_EOL;

    // SHOW THE STRING WE MADE
    echo htmlentities($xml);

    // MAKE AN OBJECT
	$obj = SimpleXML_Load_String($xml);

	// ACTIVATE THIS TO VISUALIZE THE OBJECT
	// var_dump($obj);

	// ITERATE OVER THE EXPECTED VALUES AND THE OBJECT
	foreach ($expected as $field)
	{
	    $thing = (string)$obj->$field;
	    if (empty($thing))
	    {
	        echo PHP_EOL . "THE $field IS EMPTY";
	    }

	}
}
// DONE WITH PHP PROCESSING - PUT UP THE FORM
?>

<form method="post">
USERNAME:  <input type="text"   name="username"  />
USERTOKEN: <input type="text"   name="usertoken" />
<input type="hidden" name="requesttype" value="user" />
<input type="submit" name="unused"      value="Make XML" />
</form>

Open in new window

KrzyviewAuthor Commented:
I will set this up and let you know how well I did in a little while. Also Thank you for all your help so far.
KrzyviewAuthor Commented:
I got this response:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
     
-
<response>
<status>FAILED</status>
-
<errormessage>
The XML you provided is not valid. Please check your XML document and try again.
</errormessage>
</response>

I modified the code as such:

<?php // RAY_temp_krzyview.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// EXPECTED DATA - YOU MIGHT WANT TO ADD SOME SANITY CHECKS ON THE INPUT
$expected = array ( 'My User Name for API Key', 'MY API Key', 'user','createnewuser' );

// HAS ANYTHING BEEN POSTED TO THIS SCRIPT
if (!empty($_POST))
{
    // USE THE POSTED KEYS AND DATA TO CREATE XML STRING
    $xml = '<?xml version="1.0"?>' . PHP_EOL . '<xmlrequest>' . PHP_EOL;
    foreach ($_POST as $key => $data)
    {
        // ONLY USE THE EXPECTED INPUT FIELDS
        if (in_array($key, $expected))
        {
            // CREATE AN XML TAG AND VALUE
            $xml .= "<$key>" . $data . "</$key>" . PHP_EOL;
        }
    }

    /// END OF CREATING XML - CLOSE THE XML STRING
    $xml .= '</xmlrequest>' . PHP_EOL;

    // SHOW THE STRING WE MADE
    echo htmlentities($xml);

    // MAKE AN OBJECT
      $obj = SimpleXML_Load_String($xml);

      // ACTIVATE THIS TO VISUALIZE THE OBJECT
      // var_dump($obj);

      // ITERATE OVER THE EXPECTED VALUES AND THE OBJECT
      foreach ($expected as $field)
      {
          $thing = (string)$obj->$field;
          if (empty($thing))
          {
              echo PHP_EOL . "THE $field IS EMPTY";
          }

      }
}
// DONE WITH PHP PROCESSING - PUT UP THE FORM
?>

<form method="post" action="http://email-marketers.com/members/xml.php">
USERNAME:  <input type="text"   name="username"  />
USERTOKEN: <input type="text"   name="usertoken" />
<input type="hidden" name="requesttype" value="user" />
<input type="submit" name="unused"      value="Make XML" />
</form>
 
I don't understand really what's going on from here
KrzyviewAuthor Commented:
Ok Here is my code now:

<?php
$name .= trim(stripslashes($_GET['username']));
$romeo .= trim(stripslashes($_GET['passage']));
$fname .= trim(stripslashes($_GET['uname']));


$xml = '<xmlrequest>  
     <username>*****</username>  
     <usertoken>*****</usertoken>  
     <requesttype>user</requesttype>  
     <requestmethod>createnewuser</requestmethod>  
     <details>
    <trialuser>0</trialuser>
    <username>$name</username>
    <password>$romeo</password>
    <fullname>$fname</fullname>
    <emailaddress>trial_user@somedomain.com</emailaddress>
    <usertimezone>GMT+10</usertimezone>
    <permissions>
   
    </permissions>
    </details>  
 </xmlrequest>';  
   
 $ch = curl_init('http://www.mydomain.com/xml.php');  
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
 curl_setopt($ch, CURLOPT_POST, 1);  
 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);  
 $result = @curl_exec($ch);  
 if($result === false) {  
     echo "Error performing request";  
 }  
 else {  
     $xml_doc = simplexml_load_string($result);  
    echo 'status is ', $xml_doc->status, '<br/>';  
     if ($xml_doc->status == 'SUCCESS') {  
         print_r($result);  
        } else {  
        echo 'Error is ', $xml_doc->errormessage, '<br/>';  
     }  
 }?>  

Ok so now my problem is the variables $name, $romeo, $uname are not getting passes into being part of the $xml variable. What is registers the user at is $name. How can i actually get it to pass the choosen username of the front end consumer?

KrzyviewAuthor Commented:
Ok Here is my code now:

<?php
$name .= trim(stripslashes($_GET['username']));
$romeo .= trim(stripslashes($_GET['passage']));
$fname .= trim(stripslashes($_GET['uname']));


$xml = '<xmlrequest>  
     <username>*****</username>  
     <usertoken>*****</usertoken>  
     <requesttype>user</requesttype>  
     <requestmethod>createnewuser</requestmethod>  
     <details>
    <trialuser>0</trialuser>
    <username>$name</username>
    <password>$romeo</password>
    <fullname>$fname</fullname>
    <emailaddress>trial_user@somedomain.com</emailaddress>
    <usertimezone>GMT+10</usertimezone>
    <permissions>
   
    </permissions>
    </details>  
 </xmlrequest>';  
   
 $ch = curl_init('http://www.mydomain.com/xml.php');  
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
 curl_setopt($ch, CURLOPT_POST, 1);  
 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);  
 $result = @curl_exec($ch);  
 if($result === false) {  
     echo "Error performing request";  
 }  
 else {  
     $xml_doc = simplexml_load_string($result);  
    echo 'status is ', $xml_doc->status, '<br/>';  
     if ($xml_doc->status == 'SUCCESS') {  
         print_r($result);  
        } else {  
        echo 'Error is ', $xml_doc->errormessage, '<br/>';  
     }  
 }?>  

Ok so now my problem is the variables $name, $romeo, $uname are not getting passes into being part of the $xml variable. What is registers the user at is $name. How can i actually get it to pass the choosen username of the front end consumer?

KrzyviewAuthor Commented:
Is anyone able to help me out here and see why my variable's values are not carrying over please.
Ray PaseurCommented:
You might want to get a book about PHP.  I would recommend "PHP 5 in Practice" by Eli White. Lots of great examples, etc.

When you put a variable name in single quotes you tell PHP to do no variable substitution.  See the code snippet for an example.  So try using double quotes or HEREDOC syntax.
http://php.net/manual/en/language.types.string.php

Best regards, ~Ray
Ray PaseurCommented:
Well, we lost the code snippet again. Feh
<?php
// ASSIGN A VALUE TO A VARIABLE
$var = 'Value';

// ECHO IT TWO WAYS
echo "$var";
echo '$var';

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KrzyviewAuthor Commented:
Cool thank you. I have several books on PHP and that's how I got that far so yeah. Thank You very very much.
KrzyviewAuthor Commented:
This information was basically right on the entire time. It was my fault using the single quotes but when changing to the double quote script worked perfect.
Ray PaseurCommented:
Thanks for the points!  Best of luck with your project, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.