I wanted to view the contents of a JSON post in a web hook from an external application. In this instance the source application was Stripe posting event info at irregular intervals to a PW page URL. The process had to be unobtrusive.
Solution was to send an email to myself. The web hook page template contained:
// create a PW mail object using whatever method works for you
$mail = wire()->modules('WireMailSmtp');
// Retrieve the request's body and parse it as JSON
$stripe_input = @file_get_contents("php://input");
$event_json = json_decode($stripe_input);
try {
$body = "<pre>" . var_export($event_json, true) . "</pre>";
$mail->to('my@emailaddress.com');
$mail->from('from@emailaddress.com');
$mail->subject('test event_json');
$mail->bodyHTML($body);
$mail->send();
}
catch (\Exception $e) {
$error = "Email not sent: " . $e->getMessage();
$mail->log($error);
}
Resulting email body contains nicely formatted code, eg:
stdClass::__set_state(array(
'id' => 'evt_XXXXXXXXXXXXXXXX',
'object' => 'event',
'api_version' => '2016-07-06',
'created' => 1476900798,
'data' =>
stdClass::__set_state(array(
'object' =>
stdClass::__set_state(array(
'id' => 'sub_XXXXXXXXXXXXXXXX',
'object' => 'subscription',
'application_fee_percent' => NULL,
'cancel_at_period_end' => false,
'canceled_at' => NULL,
'created' => 1476900796,
'current_period_end' => 1508436796,
'current_period_start' => 1476900796,
'customer' => 'cus_XXXXXXXXXXXXXXXX',
'discount' => NULL,
'ended_at' => NULL,
'livemode' => true,
'metadata' =>
stdClass::__set_state(array(
)),
'plan' =>
stdClass::__set_state(array(
'id' => 'annual',
'object' => 'plan',
'amount' => 8000,
'created' => 1474521586,
'currency' => 'usd',
'interval' => 'year',
'interval_count' => 1,
'livemode' => true,
'metadata' =>
stdClass::__set_state(array(
)),
'name' => 'Annual',
'statement_descriptor' => NULL,
'trial_period_days' => NULL,
)),
'quantity' => 1,
'start' => 1476900796,
'status' => 'active',
'tax_percent' => NULL,
'trial_end' => NULL,
'trial_start' => NULL,
)),
)),
'livemode' => true,
'pending_webhooks' => 1,
'request' => 'req_XXXXXXXXXXXXXXXX',
'type' => 'customer.subscription.created',
))