Curl php post data

Here is my task
Input:
 The following URL creates a new employee
http://demo.tools.krds.com/dispatch_ws.php?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

<?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

The URL you are using redirects to a Facebook login page, are you certain that’s the one you should be using?

no Mittineague. Its not redirecting to facebook nor any page

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?

Try with query string

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

Use this full url

I did. Blank page, no mark-up returned.

HTTP headers

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?

We have to post using CURL. Please read my question

If i post using curl , it have to return OK

That’s my task

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
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

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

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.

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 :blush:

This code

$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);
var_dump($server_output);
curl_close ($ch);

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

shows

string '' (length=0)

Not Ok

while cpradio’s

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"demo.tools.krds.com/dispatch_ws.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"action=create&name=value1&type=value2");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

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

shows

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

Thanks For All Your Reply.

Yet to get the result what i want

Some please help to get exact output

So what’s the output?

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

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.