2017-09-04 89 views
1

從外部服務器的傳入POST數據我有在laravel處理傳入的POST數據的路線:如何處理Laravel

Route::get('/sendgrid/api', 'SendGrid\[email protected]'); 

這裏是我的控制器:

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); 
    } 
} 

但仍然無法正常工作。我做錯了什麼?

+1

收到'路線::得到()',也許? – Scuzzy

+0

使用':: post'並將您的網址添加到'csrf'除外。 –

+0

這沒有幫助。還是行不通。 – saintsweeto

回答

0

首先,你可以改變你的路線是這樣的

Route::any('/sendgrid/api', 'SendGrid\[email protected]'); 

然後,你必須忽略在中間件> VerifyCsrfToken

不使用CSRF,並添加您的代碼如下所示

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

而且您可以使用並更改

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

$contents = $request->getContent(); 

我希望這個代碼可以幫助你的問題。感謝

0

像你說的,這是一個POST請求,然後使用POST

Route::post('/sendgrid/api', 'SendGrid\[email protected]');