如何在Laravel中处理来自外部服务器的传入POST数据 [英] How to handle incoming POST data from external server in Laravel

查看:221
本文介绍了如何在Laravel中处理来自外部服务器的传入POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条在laravel中处理传入POST数据的路线:

I have a route for handling incoming POST data in laravel:

Route::get('/sendgrid/api', 'SendGrid\EmailEventsController@parse');

这是我的控制器:

namespace App\Http\Controllers\SendGrid;

use App\Http\Controllers\Controller;
use App\Models\SendGrid\EmailEvents;

class EmailEventsController extends Controller
{
    public function parse()
    {
        $contents = file_get_contents("php://input");
        $requests = json_decode($contents);

        $data = array();

        foreach ($requests as $request)
        {
            array_push($data, array(
                'email' => $request->email,
                'event' => $request->event,
                'category' => $request->category
            ));
        }

        EmailEvents::insert($data);
    }
}

但是仍然不起作用.我做错了什么?

But still doesn't work. What did I do wrong?

推荐答案

首先,您可以更改如下所示的路线

First, you can change your route looks like this

Route::any('/sendgrid/api', 'SendGrid\EmailEventsController@parse');

然后,您必须在中间件> VerifyCsrfToken中忽略未使用的csrf

And then, you must ignore for not used csrf in Middleware > VerifyCsrfToken

并添加您的代码如下

protected $except = [
     '/sendgrid/api',
];

您可以使用和更改

$contents = file_get_contents("php://input");

$contents = $request->getContent();

我希望这段代码可以帮助您解决问题.谢谢

i hope this code can help your problems. thanks

这篇关于如何在Laravel中处理来自外部服务器的传入POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆