How to post XML request to URL

Discussion in 'PHP' started by rafiqasad, Sep 27, 2010.

  1. #1
    when user press a submit button then a http post request should be sent to www.example.com/test/add

    encoding should be utf8 and content type must be text/xml

    request hast to be structured i.e

    <abc>
    <item>
    <subitem1>
    <subitem2>

    etc..
     
    rafiqasad, Sep 27, 2010 IP
  2. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #2
    What's the input that user need to input? XML file? XML-formatted string?
     
    xrvel, Sep 27, 2010 IP
  3. jaholden

    jaholden Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is this actually a PHP question? It sounds like the job would be done by JavaScript in the browser.

    Alternatively, if you're wanting to make your PHP script perform the POST request, you can do this with CURL.
     
    jaholden, Sep 28, 2010 IP
  4. phptechie

    phptechie Well-Known Member

    Messages:
    885
    Likes Received:
    10
    Best Answers:
    2
    Trophy Points:
    165
    #4
    If you just need to send the RAW form data , then just use the URL in the action field in the form tag
    But if you want to sent an XML request , then use CURL
     
    phptechie, Sep 28, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    You can do this easily using CURL - of course, you need to construct your XML request first;

    Something like this [untested]


       $connection = curl_init();
        curl_setopt($connection, CURLOPT_URL, $yourURLtoPostXMLtoo);
        curl_setopt($connection, CURLOPT_HTTPHEADER, $SendAnyOtherHeadersToo);
        curl_setopt($connection, CURLOPT_POST, 1);
        curl_setopt($connection, CURLOPT_POSTFIELDS, $YourPreviouslyConstructedXMLRequest);
        curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($connection);
        curl_close($connection);
    PHP:
    For more information on headers and setting other curl options as required, see the manual page;

    http://php.net/manual/en/book.curl.php
     
    lukeg32, Sep 28, 2010 IP
    jestep likes this.
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    +1 on the above answer. Once you execute this, you would then parse the $response. If they return xml you can use simplexml. If you're not sure, run the script and use a var_dump on $response.
     
    jestep, Sep 28, 2010 IP