Pregunta

I'm in need of some help gather the JSON send by Shopify within Laravel ...

Route::post('/shopify/webhook/payment', function() {

    // capture json here

});

How does one capture the JSON from the webhook? Should I be using fopen('php://input'); or is there a better way through Laravel?

¿Fue útil?

Solución

From reading the Shopify documentation, it appears that the webhook POSTs JSON directly in the body (not in a variable). So yes, under normal circumstances you'd use file_get_contents('php://input') and then json_decode that.

However, Symfony's bootup actually reads this stream, rendering it useless to you (this stream can only be read once in PHP). As such, you have to read the POST body from the Symfony Request object: $json = json_decode(Request::getContent());

Actually Laravel helps you out even more: just do $json = Request::json(); That will get you the whole JSON as a Laravel ParameterBag, or you can pass a key (and its default) in: $something = Request::json('key', 'default value');

Otros consejos

I created my helper function, which will fetch json, and send with reponse:

public function getJson($path,$type,$response)
    {
        if($type == "decoded"){
            return $this->decode(\File::get($path),$response);

        }
            return $this->encode(\File::get($path),$response);
    }

decode and encode are just abstractions over json_encode and json_decode functions. Response flag indicated if json should be sent as HTTP response, or plain text. File is Laravel wrapper around builtin PHP file handling.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top