/** * Parse the body of a PSR-7 message, into a PHP array. * TODO: We really need to find a package to do this. It is built into the * Guzzle client as helper methods, but this is not specifically a client * function. * * @param $message MessageInterface * @return array|mixed */ public static function parseBody(MessageInterface $message) { // If a ServerRequest object, then parsing will be handled (and cached if necessary) // by the implementation. if ($message instanceof ServerRequestInterface) { return $message->getParsedBody(); } $data = []; if ($message->hasHeader('Content-Type')) { // Sage Pay returns responses generally with JSON, but the notify callback is Form URL // encoded, so we need to parse both. if ($message->getHeaderLine('Content-Type') === 'application/x-www-form-urlencoded') { parse_str((string) $message->getBody(), $data); } elseif ($message->getHeaderLine('Content-Type') === 'application/json') { $data = json_decode((string) $message->getBody(), true); } } return $data; }