Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra value 1 is appending with curl response

Tags:

php

curl

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.")";
?>
like image 569
MaxEcho Avatar asked Nov 03 '13 18:11

MaxEcho


People also ask

Where does curl output to?

If not told otherwise, curl writes the received data to stdout. It can be instructed to instead save that data into a local file, using the -o, --output or -O, --remote-name options. If curl is given multiple URLs to transfer on the command line, it similarly needs multiple options for where to save them.

How do I save curl response?

These curl recipes show you how to save the response from a curl request to a file. By default, curl prints the response to screen. To make it save the response to a file, use the -o file command line option.


1 Answers

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);
like image 55
Amal Murali Avatar answered Oct 12 '22 00:10

Amal Murali