Parsed body to array/object

Achterstraat

Verified User
Joined
Mar 21, 2015
Messages
70
Location
Netherlands
I wanna make my contribution to the API Class by providing an improved fetch_parsed_body() function that convert the results in an ordered array/object

Code:
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));
}
 
Last edited:
Alex,

Thanks, i already knew that! Most people put the original httpsocket from l0rdphi1 directly on their website without reading the documentation.. ..if they read here, they've now 2 solutions!

Btw, if you see JSON, would you like to ask him to walk through the documentation? First 3 items form your ?query=json got 2 faults;

Note, there is a json=yes option for this, and almost all other calls (not always, but usually), eg:
CMD_API_ADDITIONAL_DOMAINS?action=view&domain=domain.com=yes

The CMD_SHOW_USER variant did not have all of this info, so the same "custom_items" array is now added to:
CMD_SHOW_USER?jso=yes&user=fred
 
Back
Top