Skip to content

Instantly share code, notes, and snippets.

@pointofpresence
Last active March 31, 2024 11:45
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 pointofpresence/2d2d6f71e5ad1e177aa008023394a5d1 to your computer and use it in GitHub Desktop.
Save pointofpresence/2d2d6f71e5ad1e177aa008023394a5d1 to your computer and use it in GitHub Desktop.
Как получить данные из тела запроса PUT, PATCH или DELETE?
<?php
// Получение данных из тела запроса
function getFormData($method) { 
    // GET или POST: данные возвращаем как есть
    if ($method === 'GET') return $_GET;
    if ($method === 'POST') return $_POST;
 
    // PUT, PATCH или DELETE
    $data = array();
    $exploded = explode('&', file_get_contents('php://input'));
 
    foreach($exploded as $pair) {
        $item = explode('=', $pair);
        if (count($item) == 2) {
            $data[urldecode($item[0])] = urldecode($item[1]);
        }
    }
 
    return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment