SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CURL PHP POST DATA

    Here is my task
    Input:
     The following URL creates a new employee
    http://demo.tools.krds.com/dispatch_...?action=create
     Expected POST data to create a new employee is name and type
     Web service returns HTTP 201 CREATED response code if employee creation is successful
    Output:
     Create a new employee and output “OK” to the browser if and only if creation is successful
    Guidelines
     Use PHP & CURL only

    When i tried the below code

    Code PHP:
    <?php
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL,"http://demo.tools.krds.com/dispatch_ws.php?action=create");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"name=value1&type=value2");
     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
    $server_output = curl_exec ($ch);
     
    curl_close ($ch);
     
    if ($server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; }
     
    ?>

    Its going to else part

    I tried var_dump($server_output) which shows --> string(0) ""

    Can any one help please

    Thanks in advance
    Last edited by Mittineague; Dec 10, 2013 at 00:54. Reason: reformatting bbcode tags

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    The URL you are using redirects to a Facebook login page, are you certain that's the one you should be using?

  3. #3
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no Mittineague. Its not redirecting to facebook nor any page



    Quote Originally Posted by Mittineague View Post
    The URL you are using redirects to a Facebook login page, are you certain that's the one you should be using?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Sorry, It's late here and I'm getting tired.

    The URL was a blank page, so I went to the domain demo.tools.krds.com and that redirected to a Facebook login page.

    Anyway, are you certain the URL you are trying is correct?

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try with query string

    http://demo.tools.krds.com/dispatch_...?action=create

    Use this full url

    Quote Originally Posted by Mittineague View Post
    Sorry, It's late here and I'm getting tired.

    The URL was a blank page, so I went to the domain demo.tools.krds.com and that redirected to a Facebook login page.

    Anyway, are you certain the URL you are trying is correct?

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    I did. Blank page, no mark-up returned.

    HTTP headers
    Code:
    http://demo.tools.krds.com/dispatch_ws.php?action=create
    
    GET /dispatch_ws.php?action=create HTTP/1.1
    Host: demo.tools.krds.com
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    DNT: 1
    Connection: keep-alive
    Cache-Control: max-age=0
    
    HTTP/1.1 400 Bad Request
    Server: nginx/1.4.0
    Date: Tue, 10 Dec 2013 07:36:12 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 0
    Connection: keep-alive
    X-Powered-By: PHP/5.4.21
    Again are you certain this is the URL you should be using?

  7. #7
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We have to post using CURL. Please read my question

    If i post using curl , it have to return OK

    That's my task

    Quote Originally Posted by Mittineague View Post
    I did. Blank page, no mark-up returned.

    HTTP headers
    Code:
    http://demo.tools.krds.com/dispatch_ws.php?action=create
    
    GET /dispatch_ws.php?action=create HTTP/1.1
    Host: demo.tools.krds.com
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    DNT: 1
    Connection: keep-alive
    Cache-Control: max-age=0
    
    HTTP/1.1 400 Bad Request
    Server: nginx/1.4.0
    Date: Tue, 10 Dec 2013 07:36:12 GMT
    Content-Type: text/html; charset=UTF-8
    Content-Length: 0
    Connection: keep-alive
    X-Powered-By: PHP/5.4.21
    Again are you certain this is the URL you should be using?

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, here was your mistake, you left ?action=create on your CURLOPT_URL, really you want that to be part of your CURLOPT_POSTFIELDS

    The following should get you closer to your desired result

    PHP Code:
    <?php
    $ch 
    curl_init();

    curl_setopt($chCURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);

    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; }

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Okay, here was your mistake, you left ?action=create on your CURLOPT_URL, really you want that to be part of your CURLOPT_POSTFIELDS
    More specifically, you MUST send your data via POST. Your assignment clearly states the server is expecting POST data; putting things on the URL is GET data.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    That should teach me for working while tired. I saw the GET vars in the URL and completely missed seeing "POST" all over the place

    This code
    PHP Code:
    $ch curl_init();
     
    curl_setopt($chCURLOPT_URL,"http://demo.tools.krds.com/dispatch_ws.php?action=create");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"name=value1&type=value2");
     
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
     
    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);
     
    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; } 
    shows
    Code:
    string '' (length=0)
    
    Not Ok
    while cpradio's
    PHP Code:
    $ch curl_init();

    curl_setopt($chCURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);

    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; } 
    shows
    Code:
    string '<script type="text/javascript">
    
    function $(id)
    
    {
    
    	return document.getElementById(id);
    
    }
    
    </script>
    
    
    
    <link href="http://demo.tools.krds.com/skins/default/css/style.css?v=0.1386702159" rel="stylesheet" type="text/css" />
    
    
    
    <fb:user-agent includes="ie 6">
    
    	<link href="http://demo.tools.krds.com/skins/default/css/ie6.css?v=0.1386702159" rel="stylesheet" type="text/css" />
    
    </fb:user-agent>
    
    
    
    <fb:user-agent includes="ie 7">
    
    	<link href="http://demo.tools.krds.com/skins/default/css/ie7.css?v=0.138670215'... (length=694)
    
    Not Ok

  11. #11
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Result

    Thanks For All Your Reply.

    Yet to get the result what i want

    Some please help to get exact output

    Quote Originally Posted by Mittineague View Post
    That should teach me for working while tired. I saw the GET vars in the URL and completely missed seeing "POST" all over the place

    This code
    PHP Code:
    $ch curl_init();
     
    curl_setopt($chCURLOPT_URL,"http://demo.tools.krds.com/dispatch_ws.php?action=create");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"name=value1&type=value2");
     
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
     
    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);
     
    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; } 
    shows
    Code:
    string '' (length=0)
    
    Not Ok
    while cpradio's
    PHP Code:
    $ch curl_init();

    curl_setopt($chCURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);

    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; } 
    shows
    Code:
    string '<script type="text/javascript">
    
    function $(id)
    
    {
    
    	return document.getElementById(id);
    
    }
    
    </script>
    
    
    
    <link href="http://demo.tools.krds.com/skins/default/css/style.css?v=0.1386702159" rel="stylesheet" type="text/css" />
    
    
    
    <fb:user-agent includes="ie 6">
    
    	<link href="http://demo.tools.krds.com/skins/default/css/ie6.css?v=0.1386702159" rel="stylesheet" type="text/css" />
    
    </fb:user-agent>
    
    
    
    <fb:user-agent includes="ie 7">
    
    	<link href="http://demo.tools.krds.com/skins/default/css/ie7.css?v=0.138670215'... (length=694)
    
    Not Ok

  12. #12
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what's the output?

    I was completely messed up. PLease help me out get the required output

  13. #13
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No cpradio, its just returning string(694) " Not Ok

    Yet to get the desired the output

    Quote Originally Posted by cpradio View Post
    Okay, here was your mistake, you left ?action=create on your CURLOPT_URL, really you want that to be part of your CURLOPT_POSTFIELDS

    The following should get you closer to your desired result

    PHP Code:
    <?php
    $ch 
    curl_init();

    curl_setopt($chCURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

    $server_output curl_exec ($ch);
    var_dump($server_output);
    curl_close ($ch);

    if (
    $server_output == "OK") { echo 'Ok'; } else { echo 'Not Ok'; }

  14. #14
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by psychegames.seg View Post
    No cpradio, its just returning string(694) " Not Ok

    Yet to get the desired the output
    I know what your desired output is, but what you have obviously won't provide that. I simply was showing you were not getting ANY output from your CURL response ('' empty string), so you needed to fix your CURL request so you actually got data back. Now you have data back, you need to fix your condition.


Tags for this Thread

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
  •