SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending and Recieving XML via cURL

    I'm new to sending, receiving, and reading XML. I'm trying to send and receive XML data via cURL. I am using curl because it was so easy with just arrays. I am using the following code to generate the XML and the PHP script that is presently not working. It would be great if some great code god can explain whats wrong to me.

    build_xml.php
    Code:
    <?php
    '<?xml version="1.0"?>'
    $xml = "<member><name>name</name></member>";
    echo $xml;
    ?>
    Array Method - Works great with arrays. I tried with XML as $data, and it no work. Getting it working this way is preferred.
    Code:
    <?php
    exec("/usr/bin/curl -m 120 -d \"$xml\" http://www.example.com/index.php -L", $response);
    // $response is null
    ?>
    Alternate Method - This way is failing also.
    Code:
    <?php 
    $url = "build_xml.php";
    $ch = curl_init();    // initialize curl handle
    curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
    curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
    $result = curl_exec($ch); // run the whole process
    echo $result; //contains response from server 
    // NO RESPONSE RECEIVED
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please, explain your problem. It seem you have no problem getting xml, just sending it (I can be wrong). But you have not provided the code where you are formatting xml/variables to post your xml to the server.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I posted this over on DP as well...but might as well reply here since I haven't dumped the temp file yet.
    PHP Code:
    <?php
        
    /** 
         * Define POST URL and also payload
         */
        
    define('XML_PAYLOAD''<?xml version="1.0"?><member><name>name</name></member>');
        
    define('XML_POST_URL''http://www.domain.com/build_xml.php');
            
        
    /**
         * Initialize handle and set options
         */
        
    $ch curl_init(); 
        
    curl_setopt($chCURLOPT_URLXML_POST_URL); 
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 
        
    curl_setopt($chCURLOPT_TIMEOUT4); 
        
    curl_setopt($chCURLOPT_POSTFIELDSXML_PAYLOAD); 
        
    curl_setopt($chCURLOPT_HTTPHEADER, array('Connection: close'));
        
        
    /**
         * Execute the request and also time the transaction
         */
        
    $start array_sum(explode(' 'microtime()));
        
    $result curl_exec($ch); 
        
    $stop array_sum(explode(' 'microtime()));
        
    $totalTime $stop $start;
        
        
    /**
         * Check for errors
         */
        
    if ( curl_errno($ch) ) {
            
    $result 'cURL ERROR -> ' curl_errno($ch) . ': ' curl_error($ch);
        } else {
            
    $returnCode = (int)curl_getinfo($chCURLINFO_HTTP_CODE);
            switch(
    $returnCode){
                case 
    200:
                    break;
                default:
                    
    $result 'HTTP ERROR -> ' $returnCode;
                    break;
            }
        }
        
        
    /**
         * Close the handle
         */
        
    curl_close($ch);
        
        
    /**
         * Output the results and time
         */
        
    echo 'Total time for request: ' $totalTime "\n";
        echo 
    $result;      
        
        
    /**
         * Exit the script
         */
        
    exit(0);
    ?>
    Enjoy...

    Bobby

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not see what variable are you trying to post. I can just see the value posted.

    PHP Code:
    curl_setopt($chCURLOPT_POSTFIELDS"var1=value1&var2=value2"); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dzysyak,

    This is not form data that is being POST'ed...it's an XML payload. The obvious difference is not being able to access it via normal $_POST superglobal. However, this is easily (and quickly) overcome with using the STDIN.

    On the receiving script use:
    PHP Code:
    $xmlPayload file_get_contents("php://input"); 
    Bobby

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure that POST method will allow you that. Try PUT method instead.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you serious? Do you actually know what the PUT method is for?

    I highly encourage you to read the HTTP/1.1 specification paying particular attention to sections 9.5 [POST] and 9.6 [PUT].

    I'll reserve further discussion about the core differences between POST/PUT until you have a chance to read the specification.

    RE: using raw POST payload -> it is allowed and is used widely by such companies as UPS (for rate requests) and others. The specification says that "The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI.". Even though your web server and as a consequence PHP doesn't create nice little arrays of the data does not mean it is not allowed or correct...merely that the raw data block will have to be utilized.

    Bobby

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the code that you provided with stdin looks more like PUT usage http://www.phpbuilder.com/manual/en/...put-method.php

    Not clear to me why are you asking question, if you querying some service, than you can just read manual.
    If you are developing your own service, probably you will find helpful this url: http://ua2.php.net/http

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm going to assume there is a language barrier that is preventing you from posting intelligent responses.

    Have a good day.

    Bobby

  10. #10
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chemo View Post
    I'm going to assume there is a language barrier that is preventing you from posting intelligent responses.

    Have a good day.

    Bobby
    Are you serious? Maybe its just a social barrier that prevents you from doing anything else besides waiting for me to respond. Thanks fro the input, but i do have a life and doent revolve around responding exactly when you do. Man...

    Thanks for the other response everyone. I will implement them and return with a solution.

    C.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cesarcesar View Post
    Are you serious? Maybe its just a social barrier that prevents you from doing anything else besides waiting for me to respond. Thanks fro the input, but i do have a life and doent revolve around responding exactly when you do. Man...

    Thanks for the other response everyone. I will implement them and return with a solution.

    C.
    That post ws directed at the individual above who apparently thinks the PHP manual is an ample replacement for HTTP/1.1 specifications. However, I appreciate your kind comment. Also, you're welcome for my code to solve your problem.

    Bobby

  12. #12
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i see. sorry for snappin. ive had a great day with my daughter and dammit no one is bringing me down! Anyways, i will try your code, thanks forr posting at DP as well.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chemo, I am sorry - really my mistake. I have written some code and got something similar to your one:

    PHP Code:
    // INIT CURL
    $ch curl_init();

    // SET URL FOR THE POST FORM LOGIN
    curl_setopt($chCURLOPT_URL'http://localhost/server.php');

    // ENABLE HTTP POST
    curl_setopt ($chCURLOPT_POST1);
    curl_setopt ($chCURLOPT_POSTFIELDS'Hello world - xml goes here');
    curl_setopt ($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_HTTPHEADER, Array("Content-Type: application/xml"));
    //curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Accept: application/xml"));
    curl_setopt($chCURLOPT_RETURNTRANSFER1);

    // EXECUTE 1st REQUEST (LOGIN)
    $postresponse curl_exec ($ch);
    echo 
    $postresponse;

    // CLOSE THE SESSION
    curl_close($ch); 
    And server side for tests:
    PHP Code:
    echo file_get_contents("php://input"); 

  14. #14
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chemo's solution works perfectly. Tahnks much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •