Commit 479fcc68 by Alfiro Pratama

Fix API Mhs

parent d03e980b
...@@ -5,6 +5,7 @@ namespace App\Http\Controllers; ...@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Exception; use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
class GetDataApiController extends Controller class GetDataApiController extends Controller
{ {
...@@ -28,20 +29,55 @@ class GetDataApiController extends Controller ...@@ -28,20 +29,55 @@ class GetDataApiController extends Controller
public static function getAccount($nim) public static function getAccount($nim)
{ {
// $client = new Client();
// $apiRequest = $client->request('POST', GetDataApiController::URL, [
// 'form_params' =>
// [
// 'username' => $nim,
// 'kondisi' => 'cekhakakses'
// ]
// ]);
// $gcon = utf8_encode($apiRequest->getBody()->getContents());
// $data = unserialize($gcon);
// return $data;
try {
$client = new Client(); $client = new Client();
$apiRequest = $client->request('POST', GetDataApiController::URL, [ $apiRequest = $client->request('POST', GetDataApiController::URL, [
'form_params' => 'form_params' => [
[
'username' => $nim, 'username' => $nim,
'kondisi' => 'cekhakakses' 'kondisi' => 'cekhakakses'
] ]
]); ]);
$gcon = utf8_encode($apiRequest->getBody()->getContents()); $response = $apiRequest->getBody()->getContents();
$data = unserialize($gcon);
// Coba unserialize langsung tanpa utf8_encode
$data = @unserialize($response);
if ($data !== false) {
return $data; return $data;
} }
// Kalau unserialize gagal, coba cek apakah data berupa JSON
$json = json_decode($response, true);
if ($json !== null) {
return $json;
}
// Log jika data tidak bisa di-unserialize dan bukan JSON
Log::error("Gagal memproses response API untuk NIM: $nim\nResponse:\n$response");
return null;
} catch (\Exception $e) {
// Log exception jika gagal request
Log::error("Exception saat memanggil API untuk NIM: $nim\n" . $e->getMessage());
return null;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment