SecondCity Posted April 14, 2017 Share Posted April 14, 2017 (edited) 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? Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
NigelRel3 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) 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. Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) 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); Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) Why does this website chop off 80% of my replies?? Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
NigelRel3 Posted April 14, 2017 Share Posted April 14, 2017 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'. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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); Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) I was concerned that all of those request values might somehow end up hanging around in the XML template that I get back from the payment gateway. So when I run this... $response = new SimpleXMLElement($xml_response); Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 Every time I try and post responses here they get CHOPPED OFF Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 Use a different browser. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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? Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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?? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) 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); Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 (edited) 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.) Edited April 14, 2017 by SecondCity Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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) Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2017 Share Posted April 14, 2017 (edited) 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? Edited April 14, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
SecondCity Posted April 14, 2017 Author Share Posted April 14, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.