3

I am trying to execute a url and getting its response. Following is the code that executes the curl. I want the curl execution to return me a string in $result.

<?php
$fields = array
(
'username'=>urlencode($username),
'pwrd'=>urlencode($pwrd),
'customer_num'=>urlencode($customer_num)
);

$url = 'http://localhost/test200.php';
//open connection
set_time_limit(20);
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
echo $result; //I want $result to be "Successful"
?>

This is my test200.php on localhost:

<?php
$usernam = $_POST['username'];
$pass = $_POST['pwrd'];
$customer_num = $_POST['customer_num'];
echo "Successful!";
?>

What changes do I make in test200.php? Please help.

1
  • How do i do that? (m new here.) Commented Apr 25, 2012 at 8:25

3 Answers 3

2

You should use the httpcode returned by the curl execution and not rely on a string that is returned

$res = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Here - http://www.webmasterworld.com/forum88/12492.htm

Sign up to request clarification or add additional context in comments.

Comments

1

Once the data is sent to test200.php do the appropriate manipulation like insert the posted values into a table and on success

echo "Successful!";

or print the same in your test200.php.. assuming you are doing an insert code in test200.php code would be like

<?php
$qry = "INSERT INTO `your_table` (`field_customer_name`, `field_username`, `field_password`) VALUES ($fields['customer_num'], $fields['username'], some_encrypt_fxn($fields['pwrd']))";
mysql_query($qry);
$err_flag = mysql_error($your_conn_link);
if($err_flag == '') {
  echo "Successful!";
}
else {
  echo "Failed, Error " . $err_flag;
}
?>

If the purpose of getting "Successful!" is to check if the cURL returns success then i suggest using Prateik's answer of using the returned status code

2 Comments

the code gets executed well. I have checked that.I just want the test200.php to return a string. Based on the returned string, I have to execute next piece of code.
Somehow a simple print("Successful"); statement in test200.php worked well. The response i get now is as follows: HTTP Code: 0 Array ( [0] => [1] => Successful )
0

Somehow a simple print("Successful"); statement in test200.php worked well. The response i get now is as follows: HTTP Code: 0 Array ( [0] => [1] => Successful )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.