#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171

    Sending request with curl and responding with XML


    Hello I am thinking of sending the request info (could be as simple as an simple id, or could be a users personal info) and create a responce with XML.

    Here are my questions:

    1 - How can I create the responce in target page (test_curl.php)? I know how to simply generate xml from the database based on the request value in the form of GET but I don't know how to send it back to this page. So please show me how to send the xml responce back to this page (either with success or fail responce).

    2 - Is this the secure way of sending username and password on a non SSL page? If not what do you recommend?

    3 - If this doesn't need to be secure at all (for example a public page), is this the right arrpoach?

    4 - Can I use the same approach and send my request in a form of xml?
    PHP Code:
    $username 'username@yahoo.com' 
    $password 'password';
    $search_id '1';
    $search_value '154';
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"http://www.webmoosh.com/test_curl.php?username=".$username."&password=".$password."&update_id=".$update_id."&update_value=".$update_value);
    curl_setopt($chCURLOPT_GETTRUE);
    $responce curl_exec($ch);
    curl_close($ch);
    echo 
    $responce
    Thank you
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    1. Output it. Then use the CURLOPT_RETURNTRANSFER setting in the first script.

    2. Uh, no? Don't send the password at all: use it and/or some other secret information to create a hash of the request (like including the URL requested), send that hash, then have the receiving end calculate the hash independently and verify the two match.

    3. If there's a password involved then it needs to be secure.

    4. Sure. You can send whatever you want to send in the request, and send whatever you want to send in the response.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by requinix
    1. Output it. Then use the CURLOPT_RETURNTRANSFER setting in the first script.
    This shows a blank page! Please guide. Thanks
    PHP Code:
    error_reporting(E_ALL);
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"http://www.webmoosh.com/REST/responce.php?update_value=12";
    curl_setopt($chCURLOPT_GETTRUE);
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    $responce curl_exec($ch);
    curl_close($ch);
    echo 
    $responce
    responce.php
    PHP Code:
    <?php error_reporting(E_ALL); header("Content-type: text/xml; charset=utf-8"); 
        
    //Just some dummy xml to test if response is working
        
    if($_GET['update_value']==12)
            {
                
    $xml"<root id=\"".$_GET['update_value']."\"><book>success</book></root>";
                echo 
    $xml;
            }
        else
            {
                
    $xml"<root id=\"".$_GET['update_value']."\"><book>Failed</book></root>";
                echo 
    $xml;    
            }
    ?>
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    You remember the leading cause of white pages, right?
    PHP Code:
    curl_setopt($chCURLOPT_URL"http://www.webmoosh.com/REST/responce.php?update_value=12"
    Your XML should have a <?xml too.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by requinix
    You remember the leading cause of white pages, right?
    No. What is it?

    Ahhh wait!!! ). What a mistake
    Last edited by zxcvbnm; March 17th, 2013 at 07:51 PM.
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Requinx:

    Ok it generates a valid xml of the results.

    Now 2 things:

    1 - It looks like echo $xml; in responce.php is what prints the xml on the screen. What was the point of curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); then?

    2 - I need to see the xml response header for example HTTP/101: 200 OK. How can I see that?

    Thank you
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    1. Without it, cURL will output whatever it receives. Your $responce would just be a boolean for success or failure.

    2. Try the CURLOPT_HEADER option. The curl_setopt() manual page lists all the options with descriptions of what they do...
  14. #8
  15. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by requinix
    1. Without it, cURL will output whatever it receives. Your $responce would just be a boolean for success or failure.
    Thanks. Yes. Without it I get 0 or 1.

    With it get nothing!

    In your original answer you mentioned "use the CURLOPT_RETURNTRANSFER". Why did you say that? Because at the moment without it I am getting the xml responce (and header info). Why would I use it anyways?
    What is it that I can't achieve without it?
  16. #9
  17. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Well, it does seem like you're completely missing how the whole thing works.

    Yes, your index2.php script outputs stuff. That's great. But how does the other script, the one with cURL, get that same stuff? Naturally by using cURL. Without CURLOPT_RETURNTRANSFER you will execute the request and cURL will directly output everything it received. You will not get that output into a variable (without silly workarounds).
    So you use that one option and now you'll get the output into a variable instead of being directly outputted to the screen. The fact that you then echo it a couple lines later just means it's harder for you to tell that there's a difference with or without it.
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Originally Posted by requinix
    Yes, your index2.php script outputs stuff. That's great. But how does the other script, the one with cURL, get that same stuff? Naturally by using cURL.
    I think you are wrong too cause I confused you. index2.php IS the one with cURL and it does NOT print anything.
    Originally Posted by requinix
    Without CURLOPT_RETURNTRANSFER you will execute the request and cURL will directly output everything it received.
    No it doesnt
    PHP Code:
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"http://www.webmoosh.com/REST/responce.php?update_value=12");
    curl_setopt($chCURLOPT_GETTRUE);
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
    echo 
    $responce curl_exec($ch);
    curl_close($ch); 
  20. #11
  21. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    FWIW, response is spelled with an s; I'm pretty sure that doesn't vary across English dialects.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  22. #12
  23. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171
    Ok I think I killed the thread by claiming requinx is wrong . This I assume is the correct way of storing the response in a variable.
    PHP Code:
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"http://www.webmoosh.com/REST/responce2.php?update_value=12");
    curl_setopt($chCURLOPT_GETTRUE);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    $responce curl_exec($ch);
    $xml simplexml_load_string($responce);
    print_r($xml);
    curl_close($ch); 
    Clearly removing curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); does not store the response in the variable and as a result nothing goes in $xml variable. Saying that I still wonder why without RETURNTRANSFER the silly workaround shows the xml!
  24. #13
  25. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Playing EVE, sometimes I get distracted.

    The "silly workaround" I was talking about is output buffering.
    If you're referring to your older code, without CURL_SETOPT, it shows the XML because cURL is outputting it. Not your echo.

IMN logo majestic logo threadwatch logo seochat tools logo