Get response in string format

I am trying to execute url and get its answer. Below is the code that executes curl. I want 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 can I make to test200.php? Please, help.

+3
source share
3 answers

Somehow a simple font ("Successful"); The expression in test200.php worked. The answer I got now is the following: HTTP code: 0 Array ([0] => [1] => Success)

0
source

You should use the httpcode returned by curl and not rely on the 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

+2
source

Once the data has been sent to test200.php , perform the appropriate manipulations, for example, insert the published values ​​into the table and successfully complete

echo "Successful!";

or type the same in your test200.php .. Assuming you do the paste code in the test200.php code, it will look 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 goal is getting "Successful!" to check if cURL returns success, then I suggest using Prateik's answer to using the returned status code

+1
source

All Articles