简体   繁体   中英

laravel file_get_contents('php://input') not working

I've searched for answers on this issue but i can't seem to find any solution.
I am trying to get response from an api service my application is consuming.
On every successful transaction, the api will push a POST notification to any url i provide with details of the transaction. I found out that only get requests go through. However, if i route the request through the public folder, it seem to work fine.
Is there any way to get file_get_contents('php://input') work using POST route controller in laravel?
Here is a sample of my code:

ApiController.php

public function recieve_payment(Request $request){
    $res = file_get_contents('php://input');
    if (!empty($res)) {
        // insert values of $res to database
    }
}

routes/web.php

Route::post('/recieve', 'ApiController@recieve_payment');

I figured to exclude the URI from CSRF protection

protected $except = [
    'stripe/*',
];

That small piece of code saved me hours literally.

Place your route in routes/api.php instead of routes/web.php. routes/api.php has CSRF disabled.

routes/api.php

Route::post('/recieve', 'ApiController@recieve_payment');

Your webhook URL will now be:

BASE_URL/api/receive

Also, you don't need to use:

$res = file_get_contents('php://input');

You can use:

$res = $request->all();

use $request->getContent()

laracast .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM