Created
October 17, 2024 16:05
-
-
Save emulsion-io/3f1a854d02066284209b07360d81de33 to your computer and use it in GitHub Desktop.
Ajoute la prise en compte de ->input->json() dans Codeingniter 3
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 | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class MY_Input extends CI_Input { | |
/** | |
* Instance de CI_Security | |
* | |
* @var CI_Security | |
*/ | |
protected $security; | |
/** | |
* Constructeur | |
* | |
* @param CI_Security $security | |
*/ | |
public function __construct(CI_Security &$security) | |
{ | |
$this->security = $security; | |
parent::__construct($this->security); | |
} | |
/** | |
* Retourne les données JSON du flux d'entrée sous forme de tableau ou d'objet | |
* | |
* @param string $index Index de l'élément à retourner | |
* @param bool $xss_clean Nettoyer les données | |
* @return mixed | |
*/ | |
public function json($index = NULL, $xss_clean = FALSE) | |
{ | |
// Lire le flux d'entrée brut | |
$input = $this->raw_input_stream; | |
// Tenter de décoder le JSON | |
if (empty($input)) { | |
log_message('error', 'Le flux d\'entrée JSON est vide.'); | |
return null; | |
} | |
$data = json_decode($input, TRUE); | |
// Vérifier si le JSON a été correctement décodé | |
if (json_last_error() !== JSON_ERROR_NONE) { | |
log_message('error', 'Erreur lors du décodage JSON : ' . json_last_error_msg()); | |
return null; | |
} | |
return $this->_fetch_from_array($data, $index, $xss_clean); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment