user1051505
user1051505

Reputation: 962

Getting a curl response in string format

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.

Upvotes: 3

Views: 15795

Answers (3)

user1051505
user1051505

Reputation: 962

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 )

Upvotes: 0

optimusprime619
optimusprime619

Reputation: 764

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

Upvotes: 1

Pratik Mandrekar
Pratik Mandrekar

Reputation: 9568

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

Upvotes: 2

Related Questions