From following two files I am getting output (2000)1 but It should only (2000)

After getting value using curl extra 1 is appending, but why?

balance.php

<?php
$url = "http://localhost/sms/app/user_balance.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, "id=2&status=Y");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>

user_balance.php

<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("sms",$conn);
$user_id = $_REQUEST["id"];
$sql = "SELECT * FROM user_sms WHERE user_id='$user_id'";
$rec = mysql_query($sql);
if($row = mysql_fetch_array($rec)) {
    $balance = $row["user_sms_balance"];
}
echo "(".$balance.")";
?>
Answer

From the PHP manual documentation for curl_setopt():

CURLOPT_RETURNTRANSFER - Set value to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

If you don't set CURLOPT_RETURNTRANSFER option to TRUE , then the return value from curl_exec() will be the boolean value of the operation -- 1 or 0. To avoid it, you can set CURLOPT_RETURNTRANSFER to TRUE, like below:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  • 14
Reply Report
      • 2
    • I ran into this problem where I was trying to do a json_decode() from a curl_exec response; I kept getting bool(true) as my result. This option cleared that error and gave me the JSON I was expecting.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions