Created
June 8, 2021 17:48
-
-
Save jeremeamia/fe7f3bcc951d4b455635eff4c7c54dc5 to your computer and use it in GitHub Desktop.
Quick and dirty Slack request parsing in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parse_slack_request(string $body): array | |
{ | |
if (empty($body)) { | |
return []; | |
} | |
if ($body[0] === '{') { | |
$data = json_decode($body, true); | |
} else { | |
parse_str($body, $data); | |
} | |
if (isset($data['payload'])) { | |
$data = json_decode(urldecode($data['payload']), true); | |
} | |
return $data; | |
} | |
// Example | |
$body = file_get_contents('php://input'); | |
$data = parse_slack_request($body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment