Jump to content

Updating an XML template


SecondCity

Recommended Posts

I have an XML template that starts off like this...

$xmlStr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <merchantAuthentication></merchantAuthentication>
  <transactionRequest>
    <transactionType>authCaptureTransaction</transactionType>
    <amount>$amount</amount>
    <payment>
      <creditCard>
      :
      :

According to the API guide, <merchantAuthentication> has these two components: name, transactionID.

 

In order to add values to this template for <merchantAuthentication>, these two lines of code seem to work...

$xml->merchantAuthentication->addChild('name', $apiLoginID);
$xml->merchantAuthentication->addChild('transactionID', $apiTransactionID);

Now on to my question...

 

I would like to remove this code...

    <amount>$amount</amount>

and instead use code like above to populate the $amount, but when I try the code it fails...

 

$xml->transactionRequest->addChild('amount', $amount);

 

 

Why doesn't this work?

 

 

 

 

 

 

Link to comment
Share on other sites

Can you post the actual code (or at least a cut down version) which shows the problem your having.  This should work, but without the details it's difficult to comment on.

 

Well, what I posted above is the essence of things?  (Since this is for an ecommerce site, I am leery to post too much.)

 

Apparently my approach above isn't right.

 

It says:  invalid child element 'employeeId' in namespace

Link to comment
Share on other sites

Why do you even want to insert data with document tree manipulations? A plain string is much more readable. Just make sure you escape the data with htmlspecialchars() and the ENT_XML1 flag.

 

An even smarter approach would be an external template and an actual template engine which handles the insertions for you. Twig is very flexible in that regard. I've personally used it even for “exotic” languages like TeX.

Link to comment
Share on other sites

Why do you even want to insert data with document tree manipulations? A plain string is much more readable. Just make sure you escape the data with htmlspecialchars() and the ENT_XML1 flag.

 

An even smarter approach would be an external template and an actual template engine which handles the insertions for you. Twig is very flexible in that regard. I've personally used it even for “exotic” languages like TeX.

 

Originally I used embedded PHP variables like this...

$xmlStr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <merchantAuthentication></merchantAuthentication>
<employeeId></employeeId>
  <transactionRequest>
    <transactionType>authCaptureTransaction</transactionType>
    <payment>
      <creditCard>
        <cardNumber>$ccNumber</cardNumber>
        <expirationDate>$ccExpDate</expirationDate>
        <cardCode>$ccSecurityCode</cardCode>
      </creditCard>
    </payment>

Here is my concern...

 

After building the XML string  run this to create an object...

$xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING);

And then when I run cURL I use this...

 

$xml_response = curl_exec($ch);
 

$response = new SimpleXMLElement($xml_response);

 

 

My payment gateway is apparently sending back the response and storing it in $xml_response.

 

And I worried that when I run the last line, that $response might contain data from the initial request, so I wanted to strip out the embedded PHP variables in my template and add the data using the ->addChild approach.

Link to comment
Share on other sites

And your concern is ... what?

 

Like I said, variables must be XML-escaped. Otherwise this is a perfectly valid approach and much simpler than jumping around in the parsed XML tree.

 

I don't know or understand OOP.

 

If I take an XML template that has variables in it - and thus values in it - and I run it through...

 

 

$xml_response = curl_exec($ch);
Link to comment
Share on other sites

Well, what I posted above is the essence of things?  (Since this is for an ecommerce site, I am leery to post too much.)

 

Apparently my approach above isn't right.

 

It says:  invalid child element 'employeeId' in namespace

Hmmm... as employeeId wasn't even mentioned in your first post, I can't see how the original code 'is the essence of things'.

Link to comment
Share on other sites

And your concern is ... what?

 

Like I said, variables must be XML-escaped. Otherwise this is a perfectly valid approach and much simpler than jumping around in the parsed XML tree.

 

I don't know or understand OOP.

 

If I take an XML template that has variables in it - and thus values in it - and I run it through...

 

 

$xml_response = curl_exec($ch);
Link to comment
Share on other sites

And your concern is ... what?

 

Like I said, variables must be XML-escaped. Otherwise this is a perfectly valid approach and much simpler than jumping around in the parsed XML tree.

 

My concern is that the variables embedded in my XML template might get returned in the payment gateway's response to me when I use this code...

 

 

        $xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING);
 
                    $xml_response = curl_exec($ch);
 
                    $response = new SimpleXMLElement($xml_response);
 
Link to comment
Share on other sites

There are no variables in the XML document. Building the XML string is handled by PHP before you even pass it to the simplexml_load_string() function. At that time, all variables have already been replaced with their corresponding values.

 

Print $xmlStr and see for yourself.

Link to comment
Share on other sites

There are no variables in the XML document. Building the XML string is handled by PHP before you even pass it to the simplexml_load_string() function. At that time, all variables have already been replaced with their corresponding values.

 

Print $xmlStr and see for yourself.

 

If I have this...

 

        $xmlStr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <merchantAuthentication></merchantAuthentication>
  <transactionRequest>
    <transactionType>authCaptureTransaction</transactionType>
    <payment>
      <creditCard>
        <cardNumber>$ccNumber</cardNumber>
        <expirationDate>$ccExpDate</expirationDate>
        <cardCode>$ccSecurityCode</cardCode>
      </creditCard>
    </payment>
 
  </transactionRequest>
</createTransactionRequest>
XML;

        $xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING);

 

Then my XML string would have things like $amount, $CC_number, etc, right?

 

When I run this, do I have to worry about $response containing any of the values above??

 

                    $xml_response = curl_exec($ch);

 

 

I guess what I am still struggling with - because it is NOT in RTFM - is what happens to my original XML template when I get a response back from the payment gateway?

Link to comment
Share on other sites

I thought when I ran this code...

 

 

                    $response = new SimpleXMLElement($xml_response);

 

...that it might use the same SimpleXMLElement object that I build above for the request?

 

 

Or is $xml_response and entirely different XML document from the payment gateway?

 

 

And so that would make $response a PHP object that is unrelated to my request SimpleXMLElement??

Link to comment
Share on other sites

Since you clearly struggle with even the most basic programming concepts, I suggest you put aside your current project and at least do a PHP crash course (like the Codeacademy one). Learn the basics and play with the language for a while before you jump to complex tasks.

 

Otherwise I don't think this will be succesful. For every question we discuss, several new questions arise, and each one is more basic. I don't blame you. But I think your approach doesn't work. What we're discussing now (strings in PHP) would be somewhere in chapter 1 of a book, right after the “Hello World”. We cannot possibly cover this all in an online forum.

 

I've told you that variables in strings are replaced as soon as PHP encounters the string. I've suggested that you try it out. If none of this helps, you need to go back to the basics.

Link to comment
Share on other sites

Is there any relationship or conflict in my code between this SimpleXMLElement object which is used for the REQUEST...

        $xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING);

...and this SimpleXMLElement object which is used for the payment gateway RESPONSE...

                    $response = new SimpleXMLElement($xml_response);

I would have thought you would do this for $response...

 

        $response = simplexml_load_string($xml_response,'SimpleXMLElement', LIBXML_NOWARNING);
Link to comment
Share on other sites

Since you clearly struggle with even the most basic programming concepts, I suggest you put aside your current project and at least do a PHP crash course (like the Codeacademy one). Learn the basics and play with the language for a while before you jump to complex tasks.

 

Otherwise I don't think this will be succesful. For every question we discuss, several new questions arise, and each one is more basic. I don't blame you. But I think your approach doesn't work. What we're discussing now (strings in PHP) would be somewhere in chapter 1 of a book, right after the “Hello World”. We cannot possibly cover this all in an online forum.

 

I've told you that variables in strings are replaced as soon as PHP encounters the string. I've suggested that you try it out. If none of this helps, you need to go back to the basics.

 

I think the fact that 5 of my posts today got got truncated and you didn't see everything I said messed up a nice conversation.

 

My confusion is around the quasi-object called SimpleXMLElement which the PHP manual says is not technically an object and you have to handle differently.

 

I was concerned that there might be a collision using the same object for the request and response.

 

I know PHP and procedural coding, but what has thrown me off in all of this is that the code I got is more OOP based'

 

 

(I edited the post above.)

Link to comment
Share on other sites

None of this has anything to do with XML or OOP or whatever. It's a problem of not understanding strings, and a string is one of the most basic type in PHP and any other programming language.

 

There are no variables in $xmlStr. Print the string and see for yourself. There are no variables in the parsed XML document, there are no variables in the data which gets sent to the serivce, and there are definitely no PHP variables in the response. The variables only exist in your code and are replaced as soon as PHP builds the $xmlStr string.

 

Take this code:

<?php

$x = 'world';
$str = "Hello $x";

echo $str;

What do you think the output looks like?

 

Again, this is programming 101.

Link to comment
Share on other sites

None of this has anything to do with XML or OOP or whatever. It's a problem of not understanding strings, and a string is one of the most basic type in PHP and any other programming language.

 

There are no variables in $xmlStr. Print the string and see for yourself. There are no variables in the parsed XML document, there are no variables in the data which gets sent to the serivce, and there are definitely no PHP variables in the response. The variables only exist in your code and are replaced as soon as PHP builds the $xmlStr string.

 

Take this code:

<?php

$x = 'world';
$str = "Hello $x";

echo $str;

What do you think the output looks like?

 

Again, this is programming 101.

 

 

This thread has morphed...

 

I understand what you said above.

 

My latest posts were dealing with the objects created.

 

Objects are not strings.

 

 

public 'payment' =>

object(SimpleXMLElement)[4]

public 'creditCard' =>

object(SimpleXMLElement)[6]

public 'cardNumber' => string '5424000000000015' (length=16)

public 'expirationDate' => string '0917' (length=4)

public 'cardCode' => string '888' (length=3)

Link to comment
Share on other sites

I guess the object created here...

 

$xml = simplexml_load_string($xmlStr,'SimpleXMLElement', LIBXML_NOWARNING);
 
 
Has no relation to this object...
 
$response = new SimpleXMLElement($xml_response);
 
 
-----------------
 
And I guess the XML string I send in the request have no relationship to the XML string that the payment gateway passes back to me.
 
 
-----------------
 
If any of this was in the manual, I wouldn't be here bugging you!
 
 
Link to comment
Share on other sites

And I guess the XML string I send in the request have no relationship to the XML string that the payment gateway passes back to me.

 

Why the fuck would that even matter. The only reason why you keep bringing up the same nonsensical question over and over again is because you do not understand what I've told you.

 

Take a deep breath. Forget about the XMLOOP stuff in your head. What is your actual problem? What prevents you from getting your code done?

Link to comment
Share on other sites

Why the fuck would that even matter. The only reason why you keep bringing up the same nonsensical question over and over again is because you do not understand what I've told you.

 

Take a deep breath. Forget about the XMLOOP stuff in your head. What is your actual problem? What prevents you from getting your code done?

 

Nothing.

 

Will probably have a working website that accepts credit cards by end of the weekend.

 

Just trying to better understand things so I don't make any mistakes and leave any holes in my code.

 

You seem to flustered to help, and this thread has become too morphed.

 

Clearly we aren't understanding each other.

 

Sorry I asked.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.