dmmd01 Posted October 8, 2010 Share Posted October 8, 2010 I'm new to PHP, XML, and web development in general, so please bear with me... I'm working in a PHP environment and need to send user credentials $username and $password in XML via a HTTP post. Does that even make sense? Let me explain: I have already successfully used a method of capturing credentials, and they can be called by the use of $username and $password. I need to pass these on to an XML web service on another domain, but they need to be included in a larger piece of XML. To top it all off, I need this process to occur only when an image in a page is clicked. Here is the XML that needs to be sent (except that $username and $password are my strings from above): <xml-fragment xmlns:vp="http://my.host.com/somedirectory"> <vp:vprequest> <query>authenticateUser</query> </vp:vprequest> <vp:vpuser> <username>$username</username> <password>$password</password> <userid/> <firstname/> <lastname/> <useremail/> <assignedRoles> <roleDef/> </assignedRoles> </vp:vpuser> <vportal> <vportal_id>1</vportal_id> </vportal> </xml-fragment> I need to send it to: https://my.host.com/xmlwebapp Anything sending to the XML Web App must pass their XML data with the content type set to application/xml. I'm confused with how I should be sending this. I believe that the XML APP can receive HTTP POST, but I don't know how to write that code into PHP and then have it send it all as XML. Any help would be appreciated! Thank you! Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/ Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 You will either need a web services library, or will need to use cURL to make the call Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120169 Share on other sites More sharing options...
dmmd01 Posted October 8, 2010 Author Share Posted October 8, 2010 JL - thank you for responding. I have never used curl, but am reading about it now. I was able to find that I have curl installed on my host, so that is a start. Can you point me to a page with sample code on how something like what I am trying to do is accomplished? I'm using PHP 5.3.2 and curl 7.1.15 Again, thank you! Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120173 Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 Hi This page may help you. It is using POST data from a form, but the principle is the same as you need. http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120189 Share on other sites More sharing options...
dmmd01 Posted October 8, 2010 Author Share Posted October 8, 2010 Alright, I think you've got me going in the right direction... I've scraped together the following code... Does it make sense? If so, how do I execute the code by means of an onclick of an image in a page? Again, thank you! <?php $xml_data ='<xml-fragment xmlns:vp="http://myhost.com/somedirectory">'. '<vp:vprequest>'. '<query>authenticateUser</query>'. '</vp:vprequest>'. '<vp:vpuser>'. '<username>$username</username>'. '<password>$password</password>'. '<userid/>'. '<firstname/>'. '<lastname/>'. '<useremail/>'. '<assignedRoles>'. '<roleDef/>'. '</assignedRoles>'. '</vp:vpuser>'. '<vportal>'. '<vportal_id>1</vportal_id>'. '</vportal>'. '</xml-fragment>'; $URL = "https://myhost.com/xmlapi"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); ?> Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120201 Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 The first point is that I would assume that your receiving application will require the POST data to be in a variable=value format so your post data would need to be in the form of : $post_data = 'xml_data = '.$xml_data; and then pass that through curl. To execute that with an onclick event, you have basically 2 choices. 1) have the code in a separate file that you redirect to with your onclick, which is a bit clunky. 2) have the code in a separate file that is called from an ajax function tied to your onclick, which could then return any messages into an element on your page to indicate the result. Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120242 Share on other sites More sharing options...
dmmd01 Posted October 8, 2010 Author Share Posted October 8, 2010 I feel like I must be getting closer, but am just not there yet. I created my test.php file that is "exactly" as shown below. I tried a few test curl examples I found on the web to ensure that curl is working, and they all worked fine. When I use my browser and go to test.php with the code below, all I get is a blank screen... How can I know if the information is being sent correctly? Theoretically, when this information is passed, it should yield a response from XML API causing me to be logged in and open up the main page of that remote site. I didn't write the API so that code should be fine! I'll deal with the onclick execution once I can just get the code to work correctly. What can I do to check this code for mistakes? <?php $xml_data ='<xml-fragment xmlns:vp="http://myhost">'. '<vp:vprequest>'. '<query>authenticateUser</query>'. '</vp:vprequest>'. '<vp:vpuser>'. '<username>username</username>'. '<password>password</password>'. '<userid/>'. '<firstname/>'. '<lastname/>'. '<useremail/>'. '<assignedRoles>'. '<roleDef/>'. '</assignedRoles>'. '</vp:vpuser>'. '<vportal>'. '<vportal_id>1</vportal_id>'. '</vportal>'. '</xml-fragment>'; $post_data = 'xml_data = '.$xml_data; $URL = "https://myhost/xmlapi"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); curl_setopt($ch, CURLOPT_POSTFIELDS, "$post_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); ?> Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120262 Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 try adding this if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } else { echo 'Operation completed without any errors'; } Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120268 Share on other sites More sharing options...
dmmd01 Posted October 8, 2010 Author Share Posted October 8, 2010 When I place your if/else above the $ch = curl_init() I get notice that the Operation completed without errors... If I place the if/else below the curl_close() I get "Curl error:" Where should it be placed? If it is executing properly, I'm doubly confused because there is no response from my XMLAPI. Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120287 Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 it needs to be where you have your curl_exec as it calls curl_exec so goes instead of the straight call Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120318 Share on other sites More sharing options...
dmmd01 Posted October 8, 2010 Author Share Posted October 8, 2010 Excellent, now we are getting somewhere... I replaced as you said, and when I accessed the test.php file, I got the following error: Curl error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed I did some research, and found that a workaround for this is to add the following before curl_exec(): curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); When I added that bit, I now get "Operation completed without any errors" --- I'm guessing that means the curl is working, but unfortunately I'm still not seeing any signs that the xml api is receiving this information. If everything is reaching the API, then I'm very confused why a session with that APP isn't being initiated. What else can I do to test this? Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120399 Share on other sites More sharing options...
dmmd01 Posted October 9, 2010 Author Share Posted October 9, 2010 Is there a way that I could have curl or some other operation print out to my screen or another file what was also sent to the http post? I'd like to be able to see the exact output to know that it is being sent correctly. Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120517 Share on other sites More sharing options...
jl5501 Posted October 9, 2010 Share Posted October 9, 2010 If you test by sending it to a page that you have control over, then, as you are sending by POST, you will see it in the $_POST superglobal, so print_r($_POST) will show your data. Link to comment https://forums.phpfreaks.com/topic/215422-sending-xml-via-http-post-in-php/#findComment-1120523 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.