SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: cURL Question

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cURL Question

    Guys,

    I have the below curl that post xml data to a test page. on my test page when i try and see what has been posted via print_r($_POST) i only see the the root element and nothing more?

    how can i get the xml file i am posting or see what i am posting?

    my curl to post
    PHP Code:
    <?php
        
    /**
         * Define POST URL and also payload
         */
       /* define('XML_PAYLOAD', "<?xml version='1.0'?><member><name>name</name><last>last</last></member>");
        define('XML_POST_URL', 'http://localhost/practice/orange/test.php');
           
        /**
         * Initialize handle and set options
         */
         
    $library simplexml_load_file('library.xml'nulltrue);
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_URL'http://localhost/practice/orange/test.php');
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_TIMEOUT4);
        
    curl_setopt($chCURLOPT_POSTFIELDS$library);
        
    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);


         
        
    ?>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The post fields variable should be an array of keys to values, like they would be in a form being posted to a page. You're just setting it to $library.

    PHP Code:
    $data = array('xml' => $library);
    curl_setopt($chCURLOPT_POSTFIELDS$data); 

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I tried that already, it returns nothing?

    All I want is load the xml into the browser using simplexml, but all attempts are failing?

    Any ideas?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by load into the browser? Why not point people directly to library.xml?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement(file_get_contents('library.xml'));
    $rCurl curl_init('http://localhost/practice/orange/test.php');
    $aCurlOptions = array(
        
    CURLOPT_RETURNTRANSFER    =>    true,
        
    CURLOPT_POST            =>    true,
        
    CURLOPT_POSTFIELDS        =>    array('xml' => $oXML->asXML())
    );
    curl_setopt_array($rCurl$aCurlOptions);
    $sResponse curl_exec($rCurl);
    ?>
    It appears you're sending a SimpleXML object rather than a string...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    What do you mean by load into the browser? Why not point people directly to library.xml?
    Dan,

    i am new cURL, admiting i did not 100% understand what you said. so i figured out how to post, but need to know how to parse the xml file i am posting on the server, so i've been playing around.

    by loading into the browser i meant rendering my posted xml to see what has been posted on the server. i though one could do something like this:

    PHP Code:
    $library simplexml_load_file('library.xml'nulltrue);

    foreach (
    $library->children() as $child){
        echo 
    $child->getName() . ":<br />";

        
    // Get attributes of this element
        
    foreach ($child->attributes() as $attr){
            echo 
    '' $attr->getName() . ': ' $attr "<br />";
        }
        
        
    // Get children
        
    foreach ($child->children() as $subchild){
            echo 
    '' $subchild->getName() . ': ' $subchild "<br />";
        }
        
        foreach (
    $subchild->children() as $subchild2){
            echo 
    '' $subchild2->getName() . ': ' $subchild2 "<br />";
        }
        
    echo 
    "\n";

    can you enlighten me?

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Any luck with the code I posted?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Any luck with the code I posted?
    what do i do with your code silverbullet, your code is posting right? but how do i check what has been posted? this is the sticking point? so simple yet so confused in translation?

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yes, this is posting the XML.

    To view this on test.php use:-

    PHP Code:
    <?php
    echo htmlentities($_POST['xml']);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Yes, this is posting the XML.

    To view this on test.php use:-

    PHP Code:
    <?php
    echo htmlentities($_POST['xml']);
    ?>
    it displays nothing? i tried printing the array too, but nothing displays on test.php

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You're not trying to access test.php in your browser are you?

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So, are you posting to 'test.php' and wanting it provide some response to the calling script based on the XML?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Poster.php
    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement('<xml><username>SilverBulletUK</username></xml>');
    $rCurl curl_init('http://localhost/responder.php');
    $aCurlOptions = array(
        
    CURLOPT_RETURNTRANSFER    =>    true,
        
    CURLOPT_POST            =>    true,
        
    CURLOPT_POSTFIELDS        =>    array('xml' => $oXML->asXML())
    );
    curl_setopt_array($rCurl$aCurlOptions);
    echo 
    curl_exec($rCurl);
    ?>
    Responder.php
    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement($_POST['xml']);
    echo 
    '<strong>'$oXML->username'</strong>';
    ?>
    Visit poster.php in your browser and it will show the response from responder, follow?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You're not trying to access test.php in your browser are you?
    nope

    here is what i am doing

    client
    post-xml.php
    PHP Code:
    $library simplexml_load_file('library.xml'nulltrue);
        
    $data = array('xml' => $library);
        
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_URL'http://localhost/practice/orange/test.php');
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_TIMEOUT4);
       
    // curl_setopt($ch, CURLOPT_POSTFIELDS, $library);
        
    curl_setopt($chCURLOPT_POSTFIELDS$data); 
        
    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); 
    server:
    test.php
    PHP Code:
    //when loop through it outputs nothing
    foreach($_POST as $key => $value){
    echo 
    $value;}

    //when i print
    print_r($_POST);

    //it prints Array([xml]=>) 
    when i try silverbullets code nothing happens at all.

  15. #15
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Poster.php
    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement('<xml><username>SilverBulletUK</username></xml>');
    $rCurl curl_init('http://localhost/responder.php');
    $aCurlOptions = array(
        
    CURLOPT_RETURNTRANSFER    =>    true,
        
    CURLOPT_POST            =>    true,
        
    CURLOPT_POSTFIELDS        =>    array('xml' => $oXML->asXML())
    );
    curl_setopt_array($rCurl$aCurlOptions);
    echo 
    curl_exec($rCurl);
    ?>
    Responder.php
    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement($_POST['xml']);
    echo 
    '<strong>'$oXML->username'</strong>';
    ?>
    Visit poster.php in your browser and it will show the response from responder, follow?
    i am looking at this now.

  16. #16
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is erroring out now:

    Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\webs\phpfiles\practice\orange\test.php on line 2

    Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\webs\phpfiles\practice\orange\test.php:2 Stack trace: #0 C:\webs\phpfiles\practice\orange\test.php(2): SimpleXMLElement->__construct('<?xml version=\...') #1 {main} thrown in C:\webs\phpfiles\practice\orange\test.php on line 2

  17. #17
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Does it work with the code I provided? We'll get onto your malformed XML in a moment.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  18. #18
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is ur code sillverbullet.

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Oh, then at guess, I would say the exception is being thrown by the responder because you have magic quotes enabled.

    In turn, the exception is being echo'd out and not actually thrown by poster.

    Change responder to....

    PHP Code:
    <?php
    $oXML 
    = new SimpleXMLElement(stripslashes($_POST['xml']));
    echo 
    '<strong>'$oXML->username'</strong>';
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh right. cheers for that.

    there is only one problem why will it not work when we load the xml as a string or file? by this i mean:

    PHP Code:
    //load as string
    $xmlstr file_get_contents('library.xml');
    $library SimpleXMLElement($xmlstr)

    //load as file
    $library = new SimpleXMLElement('library.xml'nulltrue
    thanks
    dug

  21. #21
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yay!

    Is this in responder or poster?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  22. #22
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    load on poster? then how do you go about getting the posted xml on the responder?

  23. #23
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ma201dq View Post
    load on poster? then how do you go about getting the posted xml on the responder?
    If you visit responder in your browser it will throw an exception because we never sent it any XML to parse. By using poster, we load the XML and send to responder, which obtains it from the $_POST super global.

    In this example, the responder is merely a proxy. It is not meant to be accessed directly, because it relies on data being sent to it.

    I hope I'm explaining this OK.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  24. #24
    SitePoint Evangelist
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    506
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right i see now

    so that i understand this, to send an xml document to the server and for it to be parsed would we be useing cURL to do that or a different approach like soap or rest?

    i think you deserve an upgrade from SilverBullet to GoldenBulletUK

  25. #25
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Indeed.

    For all intents and purposes, this is a very basic SOAP-like server.

    You could, with a compatible SOAP server, use this exact same method. Responder is essentially just acting as a pseudo server.

    Just happy to pass on some of the SitePoint karma.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •