Utilize webhook using PHP Webhook Listener

I have a webhook from fastSpring.

The webhook sends something like below and I want to know how can I create a webhook listener to use the inflammation passed by the webhook?

How can I create the listener to capture below information please?

{
   "order":"yzaP3EQAQtCOBLhYP2ZN-Q",
   "id":"yzaP3EQAQtCOBLhYP2ZN-Q",
   "reference":"MyDomain190418-3737-54184",
   "buyerReference":null,
   "completed":true,
   "changed":1555598658633,
   "changedValue":1555598658633,
   "changedInSeconds":1555598658,
   "changedDisplay":"4/18/19",
   "language":"en",
   "live":false,
   "currency":"USD",
   "payoutCurrency":"USD",
   "invoiceUrl":"https://MyDomain.test.onfastspring.com/account/order/MyDomain190418-3737-54184/invoice",
   "account":{
      "id":"qjY3pex6TH6rj6A2f71vVg",
      "account":"qjY3pex6TH6rj6A2f71vVg",
      "contact":{
         "first":"Jassim",
         "last":"Al Rahma",
         "email":"myemail@gmail.com",
         "company":null,
         "phone":"123456789"
      },
      "language":"en",
      "country":"AE",
      "lookup":{
         "global":"XNpsaeO3QLO906bY9HcusA"
      },
      "url":"https://MyDomain.test.onfastspring.com/account"
   },
   "total":29.99,
   "totalDisplay":"USD 29.99",
   "totalInPayoutCurrency":29.99,
   "totalInPayoutCurrencyDisplay":"USD 29.99",
   "tax":0.0,
   "taxDisplay":"USD 0.00",
   "taxInPayoutCurrency":0.0,
   "taxInPayoutCurrencyDisplay":"USD 0.00",
   "subtotal":29.99,
   "subtotalDisplay":"USD 29.99",
   "subtotalInPayoutCurrency":29.99,
   "subtotalInPayoutCurrencyDisplay":"USD 29.99",
   "discount":0.0,
   "discountDisplay":"USD 0.00",
   "discountInPayoutCurrency":0.0,
   "discountInPayoutCurrencyDisplay":"USD 0.00",
   "discountWithTax":0.0,
   "discountWithTaxDisplay":"USD 0.00",
   "discountWithTaxInPayoutCurrency":0.0,
   "discountWithTaxInPayoutCurrencyDisplay":"USD 0.00",
   "billDescriptor":"FS* fsprg.com",
   "payment":{
      "type":"test",
      "cardEnding":"4242"
   },
   "customer":{
      "first":"Jassim",
      "last":"Al Rahma",
      "email":"myemail@gmail.com",
      "company":null,
      "phone":"123456789"
   },
   "address":{
      "country":"AE",
      "display":"AE"
   },
   "recipients":[
      {
         "recipient":{
            "first":"Jassim",
            "last":"Al Rahma",
            "email":"myemail@gmail.com",
            "company":null,
            "phone":"123456789",
            "account":{
               "id":"qjY3pex6TH6rj6A2f71vVg",
               "account":"qjY3pex6TH6rj6A2f71vVg",
               "contact":{
                  "first":"Jassim",
                  "last":"Al Rahma",
                  "email":"myemail@gmail.com",
                  "company":null,
                  "phone":"123456789"
               },
               "language":"en",
               "country":"AE",
               "lookup":{
                  "global":"XNpsaeO3QLO906bY9HcusA"
               },
               "url":"https://MyDomain.test.onfastspring.com/account"
            },
            "address":{
               "country":"AE",
               "display":"AE"
            }
         }
      }
   ],
   "notes":[

   ],
   "items":[
      {
         "product":"featured-ad",
         "quantity":1,
         "display":"Featured Ad",
         "sku":null,
         "subtotal":29.99,
         "subtotalDisplay":"USD 29.99",
         "subtotalInPayoutCurrency":29.99,
         "subtotalInPayoutCurrencyDisplay":"USD 29.99",
         "discount":0.0,
         "discountDisplay":"USD 0.00",
         "discountInPayoutCurrency":0.0,
         "discountInPayoutCurrencyDisplay":"USD 0.00",
         "fulfillments":{

         }
      }
   ]
}
<?php
$data = json_decode(file_get_contents("php://input"));
?>
1 Like

I tried below code but showing nothing although webhook was already generated and I can view it from my FastSpring panel.

<?php
  header("Content-Type: application/json");

  if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
  }

  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);

 
    $data = json_decode(file_get_contents("php://input"));

    echo $data;
?>

What about if you

var_dump($data);

instead?

Perhaps try it before you output all those headers, too. Not sure if that would make a difference, but worth a go.

At the very least, put the error enabling stuff before the headers, that way you know if those are throwing problems.

I am getting NULL with var_dump

I removed all headers…

<?php /* header("Content-Type: application/json"); if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } * */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); $data = json_decode(file_get_contents("php://input")); var_dump($data); ?>

… and how are you ‘getting’ this?

My point being… if you’re just “I opened my browser and went to the page”… then yes, data is null, because you arent sending any valid JSON data along with your request.

If you believe you are sending some data along with it, then var_dump(file_get_contents("php://input"));
and see what’s sitting in the input buffer.

still nothing

string(0) “”

but my json was already generated as per FastSpring:

{  
   "order":"RRBlpWXmR5mNGDEQbhcN2w",
   "id":"RRBlpWXmR5mNGDEQbhcN2w",
   "reference":"xxxxx190419-3737-53195",
   "buyerReference":null,
   "completed":true,
   "changed":1555697031069,
   "changedValue":1555697031069,
   "changedInSeconds":1555697031,
   "changedDisplay":"4/19/19",
   "language":"en",
   "live":false,
   "currency":"USD",
   "payoutCurrency":"USD",
   "invoiceUrl":"https://xxxxx.test.onfastspring.com/account/order/xxxxx190419-3737-53195/invoice",
   "account":{  
      "id":"qjY3pex6TH6rj6A2f71vVg",
      "account":"qjY3pex6TH6rj6A2f71vVg",
      "contact":{  
         "first":"Jassim",
         "last":"Al Rahma",
         "email":"1234@gmail.com",
         "company":null,
         "phone":"123456"
      },
      "language":"en",
      "country":"AE",
      "lookup":{  
         "global":"XNpsaeO3QLO906bY9HcusA"
      },
      "url":"https://xxxxxx.test.onfastspring.com/account"
   },
   "total":29.99,
   "totalDisplay":"USD 29.99",
   "totalInPayoutCurrency":29.99,
   "totalInPayoutCurrencyDisplay":"USD 29.99",
   "tax":0.0,
   "taxDisplay":"USD 0.00",
   "taxInPayoutCurrency":0.0,
   "taxInPayoutCurrencyDisplay":"USD 0.00",
   "subtotal":29.99,
   "subtotalDisplay":"USD 29.99",
   "subtotalInPayoutCurrency":29.99,
   "subtotalInPayoutCurrencyDisplay":"USD 29.99",
   "discount":0.0,
   "discountDisplay":"USD 0.00",
   "discountInPayoutCurrency":0.0,
   "discountInPayoutCurrencyDisplay":"USD 0.00",
   "discountWithTax":0.0,
   "discountWithTaxDisplay":"USD 0.00",
   "discountWithTaxInPayoutCurrency":0.0,
   "discountWithTaxInPayoutCurrencyDisplay":"USD 0.00",
   "billDescriptor":"FS* fsprg.com",
   "payment":{  
      "type":"test",
      "cardEnding":"4242"
   },
   "customer":{  
      "first":"Jassim",
      "last":"Al Rahma",
      "email":"1234@gmail.com",
      "company":null,
      "phone":"123456"
   },
   "address":{  
      "country":"AE",
      "display":"AE"
   },
   "recipients":[  
      {  
         "recipient":{  
            "first":"Jassim",
            "last":"Al Rahma",
            "email":"1234@gmail.com",
            "company":null,
            "phone":"123456",
            "account":{  
               "id":"qjY3pex6TH6rj6A2f71vVg",
               "account":"qjY3pex6TH6rj6A2f71vVg",
               "contact":{  
                  "first":"Jassim",
                  "last":"Al Rahma",
                  "email":"1234@gmail.com",
                  "company":null,
                  "phone":"123456"
               },
               "language":"en",
               "country":"AE",
               "lookup":{  
                  "global":"XNpsaeO3QLO906bY9HcusA"
               },
               "url":"https://xxxxxx.test.onfastspring.com/account"
            },
            "address":{  
               "country":"AE",
               "display":"AE"
            }
         }
      }
   ],
   "notes":[  

   ],
   "items":[  
      {  
         "product":"featured-ad",
         "quantity":1,
         "display":"Featured Ad",
         "sku":null,
         "subtotal":29.99,
         "subtotalDisplay":"USD 29.99",
         "subtotalInPayoutCurrency":29.99,
         "subtotalInPayoutCurrencyDisplay":"USD 29.99",
         "discount":0.0,
         "discountDisplay":"USD 0.00",
         "discountInPayoutCurrency":0.0,
         "discountInPayoutCurrencyDisplay":"USD 0.00",
         "fulfillments":{  

         }
      }
   ]
}

Right… but just because it has been generated by their server doesn’t mean that your browser is SENDING that json whenever YOU visit it. The idea of a webhook is that the other server sends the request with the json; you won’t see the output, because it’s not your browser doing the requesting.

I’m trying to think of a metaphor to explain this concept.

It sounds similar to form submission. If you connect your browser to the form-processing script directly, it won’t have any of the form contents.

Think of it like this.
The webhook, comes from other server (your serve’s client), but think of them as a person visiting your website (server), normally it is the visitor who sees the response from your server in their browser after they have made a request and are served a response.
You do not see it in your browser because you are not making any request, you are not even a part of the process, it is between the client making the request and your server receiving the request and responding to it.

I did do some work last year with an API that sent webhooks that I wanted our server to respond to and act on. It was a while ago and I have slept since then.
But as I recall, to test I had to do something to trigger a webhook from the other server. But how do I know what happened?
IIRC, if logged into my account on the remote server, it would show me a record of webhooks sent out and the response sent back.
Also in my script I set it to email me the results of the script, this way I see it from both sides, client and server.
I managed to find an email:-


Authorised:
    Authorised!
Action:
    Update
Object:
    productstockdetail
Version:
    Global
Con Type:
    application/json; charset=utf-8
Con Length:
    869
Data:

    {
        "Id": 5323323,
        "ProductId": 11305452,
        "LocationId": 16435,
        "MinStock": null,
        "MaxStock": null,
        "ProductStockBatches": [
            {
                "Id": 9368394,
                "ProductStockId": 5323323,
                "CreatedDate": "2018-05-30T15:50:41.99",
                "CurrentStock": 14,
                "CurrentVolume": 0,
                "CostPrice": 458,
                "SupplierId": 104836,
                "CostPriceMeasurementSchemeItemId": null,
                "CostPriceMeasurementUnitVolume": null,
                "CostPriceUnitFactor": null,
                "CostPriceUnit": null,
                "StockMeasurementSchemeItemId": null,
                "StockUnit": null,
                "StockFactor": null,
                "MeasurementDetails": null
            },
            {
                "Id": 9496377,
                "ProductStockId": 5323323,
                "CreatedDate": "2018-06-11T13:50:08.33",
                "CurrentStock": 49,
                "CurrentVolume": 0,
                "CostPrice": 17.86,
                "SupplierId": 104836,
                "CostPriceMeasurementSchemeItemId": null,
                "CostPriceMeasurementUnitVolume": null,
                "CostPriceUnitFactor": null,
                "CostPriceUnit": null,
                "StockMeasurementSchemeItemId": null,
                "StockUnit": null,
                "StockFactor": null,
                "MeasurementDetails": null
            }
        ]
    }

The first part isn’t json, just me listing out things for information, like did the authentication actually work, the bit labelled data is the json recieved from the webhook.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.