Skip to content

Instantly share code, notes, and snippets.

@Achterstraat
Last active August 3, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Achterstraat/ac4a264c3e461e7179ba00510ef5dc13 to your computer and use it in GitHub Desktop.
Save Achterstraat/ac4a264c3e461e7179ba00510ef5dc13 to your computer and use it in GitHub Desktop.
Improved fetch_parsed_body() function that convert the results in an ordered array/object.
function fetch_parsed_body($object = false)
$result = json_decode($data, true);
if(is_null($result))
{
$result = [];
$lines = urldecode($this->result_body);
foreach(array_filter(explode("\n", $lines), 'strlen') as $line)
{
$key = (preg_match('~^([A-Z]{1,5})\=~', $line) ? strtok($line, '=') : false);
if(empty($key))
{
parse_str($line, $result);
$result = (empty($result['list']) ? $result : $result['list']);
}
else
{
$line = substr(trim($line), strlen($key)+1);
if(strlen($line) > 0)
{
preg_match_all('~([A-z0-9\.-_]+)=(?<!=["])([^&]+)(?<!=["])~', $line, $parts);
if(isset($parts[1], $parts[2]) && !empty($parts[1]) && !empty($parts[2]))
{
$line = array_combine($parts[1], $parts[2]);
}
$result[$key] = $line;
}
}
}
}
return (empty($result) ? null : ($object ? (object)$result : (array)$result));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment