I am trying to send an sms using onfo. My problem is that I new to php cURL and im not able to give the correct recipient number from my form. I need to send the waybill number and destination to the recipient. See my code below.
I am using laravel livewire.
Below is the code in the parcel.php
<?php
namespace App\Http\Livewire\Man;
use App\Models\Percel;
use App\Models\Vehicle;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class PercelComponent extends Component
{
use WithPagination;
public $sender_name;
public $sender_id_number;
public $sender_phone_number;
public $recipient_name;
public $recipient_phone_number;
public $recipient_location;
public $parcel_name;
public $parcel_quantity;
public $parcel_value;
public $parcel_destination;
public $fee_charged;
public $payment_method;
public $vehicle_id;
public $parcel_status;
public $dispatch_date;
public $user_id;
public $parcel_description;
public $waybill;
public function updated($fields)
{
$this->validateOnly($fields, [
'sender_name' => 'required',
'sender_id_number' => 'required',
'sender_phone_number' => 'required',
'recipient_name' => 'required',
'recipient_phone_number' => 'required',
'recipient_location' => 'required',
'parcel_name' => 'required',
'parcel_quantity' => 'required',
'parcel_value' => 'required',
'parcel_destination' => 'required',
'fee_charged' => 'required',
'parcel_status' => 'required',
'payment_method' => 'required',
'vehicle_id' => 'required',
'dispatch_date' => 'required',
]);
}
public function newPercel(){
$this->validate([
'sender_name' => 'required',
'sender_id_number' => 'required',
'sender_phone_number' => 'required',
'recipient_name' => 'required',
'recipient_phone_number' => 'required',
'recipient_location' => 'required',
'parcel_name' => 'required',
'parcel_quantity' => 'required',
'parcel_value' => 'required',
'parcel_destination' => 'required',
'fee_charged' => 'required',
'parcel_status' => 'required',
'payment_method' => 'required',
'vehicle_id' => 'required',
'dispatch_date' => 'required',
]);
$parcel = new Percel();
$parcel->sender_name = $this->sender_name;
$parcel->sender_id_number = $this->sender_id_number;
$parcel->sender_phone_number = $this->sender_phone_number;
$parcel->recipient_name = $this->recipient_name;
$parcel->recipient_phone_number = $this->recipient_phone_number;
$parcel->recipient_location = $this->recipient_location;
$parcel->parcel_name = $this->parcel_name;
$parcel->parcel_quantity = $this->parcel_quantity;
$parcel->parcel_value = $this->parcel_value;
$parcel->parcel_destination = $this->parcel_destination;
$parcel->fee_charged = $this->fee_charged;
$parcel->payment_method = $this->payment_method;
$parcel->vehicle_id = $this->vehicle_id;
$parcel->parcel_status = $this->parcel_status;
$parcel->dispatch_date = $this->dispatch_date;
$parcel->user_id = Auth::user()->id;
$parcel->parcel_description = $this->parcel_description;
$parcel->waybill = random_int(1000000,9999999);
$parcel->save();
$this->reset();
$this->sendMessage();
session()->flash('message', 'Parcel created successfully and the recipient has been notified via sms. View history to print receipt for packaging. Thank You.');
}
public function sendMessage()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.onfonmedia.co.ke/v1/sms/SendBulkSMS',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"SenderId": "xxxx",
"MessageParameters": [
{
"Number": "2547xxxxx541",
"Text": "Hello. Your parcel of", waybill # $parcel->waybill "will be delivered within 24hrs to $parcel->parcel_destination.Thank You."
}
],
"ApiKey": "xxxx",
"ClientId": "xxxx"
}
',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'AccessKey: xxx',
'Cookie: xx'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
return redirect()->to('/manager/percels');
}
public function render()
{
$parcels = Percel::orderBy('created_at','DESC')->paginate(5);
$sales = Percel::groupBy('user_id')->selectRaw('sum(fee_charged) as sum, user_id')->get();
$cars = Vehicle::orderBy('registration_number', 'ASC')->get();
return view('livewire.man.percel-component',['parcels'=> $parcels,'cars'=>$cars,'sales'=>$sales])->layout('layouts.admin');
}}
Please help me how I can give the phone number to receive the message to be the $recipient_phone_number, the $waybill and $parcel_destination to appear in the message.
CodePudding user response:
Try doing something like this:
$response = curl_exec($curl);
$data = json_decode($response, true);
This way you are able to create an array and access via indeces
CodePudding user response:
I suspect your variables are not being returned correctly in your sendMessage() function put a dd($parcel) and ensure you are getting your variables first. second I would place a public $parcel at the start with your other public vars.