I have a function in my laravel projrct that uses another function to send otp sms, but when I run the second function, and after return the response ,laravel return a merged response: first one is response of the second function and another one is my custom response,
my code is here:
$otp=rand(12345,99999);
VerificationCode::create([
'mobile'=>$mobile,
'otp'=>$otp,
'expire_at'=>Carbon::now()->addMinutes(10)
]);
$this->sendSms($otp,$mobile);
return response([
'result'=>'verificationCode has sent',
'message'=>'ok',
'status'=>'200'
]);
and second function is:
public function sendSms($otp,$mobile){
$post_data = http_build_query($data);
$handle = curl_init('https://example.com/api/SendSMS/BaseServiceNumber');
curl_setopt($handle, CURLOPT_HTTPHEADER, array(
'content-type' => 'application/x-www-form-urlencoded'
));
// curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$response= curl_exec($handle);
curl_close($handle);
return $response;
}
the returned response is:
{"Value":"5589489218988866748","RetStatus":1,"StrRetStatus":"Ok"}{"result":null,"message":"ok","status":"200"}
why this part ({"Value":"558948925522554445","RetStatus":1,"StrRetStatus":"Ok"}
) is added to the initial of the response?
2
Answers
The issue is caused by the
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
line in thesendSms
function. When you setCURLOPT_RETURNTRANSFER
tofalse
, thecurl_exec
function directly outputs the response instead of returning it as a string. This causes the response from the SMS API to be sent directly to the output, and then your custom response is appended to it.To fix this issue, change the
CURLOPT_RETURNTRANSFER
option totrue
:This will make
curl_exec
return the response as a string, which you can then store in the$response
variable. Since you’re not using the$response
variable in your main function, you can remove thereturn $response;
line from thesendSms
function.Now, when you call the
sendSms
function, it won’t output the SMS API response directly, and you should only see your custom response in the output.Here’s a common pattern for sending an OTP SMS and returning a response in a Laravel controller:
use IlluminateHttpRequest;
use TwilioRestClient; // Assuming you’re using Twilio for sending SMS
class MyController extends Controller
{
public function sendOtp(Request $request)
{
// Logic to generate and send OTP via SMS
$otp = generateOTP();
$phoneNumber = $request->input(‘phone_number’);
}
We generate an OTP and send it via SMS using the Twilio API.
We return a single response using response()->json(…) to indicate that the OTP was sent successfully. This is a standard way to return responses in Laravel.
Ensure that you don’t have any other code in your controller method that returns a response, as that might lead to merged responses. Review your code and make sure that only one response is being returned at the end of the method.