Jump to content

Sending XML via http post in PHP ?


dmmd01

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.