From 5c410293c843b55bcced065869e09a707f33a92f Mon Sep 17 00:00:00 2001 From: Bruno Alves Date: Tue, 16 Jan 2024 21:16:59 -0300 Subject: [PATCH] fix: json validation --- app/classes/FixJson.php | 45 +++++++------------ app/controller/faltas/faltas.php | 6 +-- .../browser.json | 1 - .../0169519f2519d15512392b5aaba489/user.json | 1 - .../browser.json | 1 + .../4b8a0682832e2fb853d0457e465d40/user.json | 1 + composer.json | 3 +- 7 files changed, 22 insertions(+), 36 deletions(-) delete mode 100644 app/sessions/0169519f2519d15512392b5aaba489/browser.json delete mode 100644 app/sessions/0169519f2519d15512392b5aaba489/user.json create mode 100644 app/sessions/4b8a0682832e2fb853d0457e465d40/browser.json create mode 100644 app/sessions/4b8a0682832e2fb853d0457e465d40/user.json diff --git a/app/classes/FixJson.php b/app/classes/FixJson.php index 2cd4052..99dfe04 100644 --- a/app/classes/FixJson.php +++ b/app/classes/FixJson.php @@ -1,5 +1,9 @@ @@ -9,37 +13,18 @@ class FixJson{ - public static function fix($string){ + public static function fix($json){ - - $curl = curl_init(); - - curl_setopt_array($curl, [ - CURLOPT_URL => "https://jsonformatter.curiousconcept.com/process?=", - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => "", - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 30, - CURLOPT_SSL_VERIFYHOST => false, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_FOLLOWLOCATION => false, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => "POST", - CURLOPT_POSTFIELDS => http_build_query(array( - "jsonfix" => "on", - "jsontemplate" => "1", - "data" => $string, - - )), - CURLOPT_HTTPHEADER => [ - "Content-Type: application/x-www-form-urlencoded" - ], - ]); - - $response = curl_exec($curl); - //echo $response; - $data = json_decode($response); - return $data->result->data; + $data = []; + $items = Items::fromString($json, ['decoder' => new ErrorWrappingDecoder(new ExtJsonDecoder())]); + foreach ($items as $key => $item) { + if ($key instanceof DecodingError || $item instanceof DecodingError) { + // handle error of this malformed json item + continue; + } + $data[$key] = $item; + } + return json_encode($data); } } \ No newline at end of file diff --git a/app/controller/faltas/faltas.php b/app/controller/faltas/faltas.php index f63a707..0ef0acb 100644 --- a/app/controller/faltas/faltas.php +++ b/app/controller/faltas/faltas.php @@ -34,7 +34,6 @@ public function __construct(){ $this->crawler = new Crawler; parent::__construct(); } - public function get(){ @@ -43,8 +42,9 @@ public function get(){ $XML_HTML = $this->getContent($this->cookie->getCookie()); $this->crawler->addHtmlContent($XML_HTML); $faltas = $this->crawler->filter('input[name="GXState"]')->attr('value'); - - $json = $this->fixJson($faltas); + + $json = FixJson::fix($faltas); + $output = $this->trataJson(json_decode($json)); $this->response($output); }else{ diff --git a/app/sessions/0169519f2519d15512392b5aaba489/browser.json b/app/sessions/0169519f2519d15512392b5aaba489/browser.json deleted file mode 100644 index 443a277..0000000 --- a/app/sessions/0169519f2519d15512392b5aaba489/browser.json +++ /dev/null @@ -1 +0,0 @@ -["ASP.NET_SessionId","d4h2bgufqij1rc55uda4zc45"] \ No newline at end of file diff --git a/app/sessions/0169519f2519d15512392b5aaba489/user.json b/app/sessions/0169519f2519d15512392b5aaba489/user.json deleted file mode 100644 index d9e74d4..0000000 --- a/app/sessions/0169519f2519d15512392b5aaba489/user.json +++ /dev/null @@ -1 +0,0 @@ -VwShJORGyf9rsX1ZGJE5WNebMr7d8HwftXddHQHSDduCfOsvtKCLW41BAqgB7N4E \ No newline at end of file diff --git a/app/sessions/4b8a0682832e2fb853d0457e465d40/browser.json b/app/sessions/4b8a0682832e2fb853d0457e465d40/browser.json new file mode 100644 index 0000000..f8e9bf8 --- /dev/null +++ b/app/sessions/4b8a0682832e2fb853d0457e465d40/browser.json @@ -0,0 +1 @@ +["ASP.NET_SessionId","hnmfhc3g2feihcetm1uotj45"] \ No newline at end of file diff --git a/app/sessions/4b8a0682832e2fb853d0457e465d40/user.json b/app/sessions/4b8a0682832e2fb853d0457e465d40/user.json new file mode 100644 index 0000000..b57d7f2 --- /dev/null +++ b/app/sessions/4b8a0682832e2fb853d0457e465d40/user.json @@ -0,0 +1 @@ +KZyWVSOoTmxNBtpeNUI2J08/6iDdqT1SZCQyt0zcnRgCCPcE2erfg9Hykcs56ZKo \ No newline at end of file diff --git a/composer.json b/composer.json index 20c942c..2605914 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "require": { "guzzlehttp/guzzle": "*", "symfony/dom-crawler": "^6.0", - "symfony/css-selector": "^6.0" + "symfony/css-selector": "^6.0", + "halaxa/json-machine": "^1.1" } }