Tim Kariuki
Tim Kariuki

Reputation: 2080

Save Post Data from External Server To Laravel

I want to save information posted to a callback URL to a Laravel database. I have created a controller and a route in the api.php file but the posted information is not being saved. When I create a PHP file in the public folder, the info is being saved but I want it to go through a controller. Kindly let me know what I need to do to save the info.

Route: Route::post('/receivesms', 'MessageController@stk');

Controller:

namespace App\Http\Controllers;

use App\Message;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function stk(Request $req){

        $message = new Message();
        $message->sender = $req->input('from');
        $message->recipient = $req->input('to');
        $message->message = $req->input('message');
        $message->date_sent = $req->input('date');
        $message->message_id = $req->input('id');
        $message->link_id = $req->input('linkId');
        $message->save();

        return view('welcome');
    }
}

The posted info is as explained here. with the posted data being;

 $from = $_POST['from'];
 $to = $_POST['to'];
 $text = $_POST['text'];
 $date = $_POST['date'];
 $id = $_POST['id'];
 $linkId = $_POST['linkId']; 

Upvotes: 0

Views: 258

Answers (1)

karmendra
karmendra

Reputation: 2243

When you put routes in api.php file api is prefixed in the route automatically refer this link Laravel Routing

so if your laravel aplication is running on https://example.com then Route::post('/receivesms', 'MessageController@stk'); will translate to https://example.com/api/receivesms

So make sure your callback your is containing api prefix, It should work.

All the Best

K

Upvotes: 1

Related Questions