Method POST di Rest API Native PHP

Saya sedang nyoba-nyoba membuat Rest API pakai PHP Native, script server API-nya begini.

$key = "CNtVA26CRRqdd293H5rdWq81Opa4UCi2GEYyulTV0zAjIytTDQ86vTvAExKqhU9P";
if(isset($_POST['key']))
{
  if ($_POST['key'] == $key)
  {
    $response = array(
      'status' => '200',
      'message' => 'Access Granted'
    );
    //http_response_code(200);
    header('Content-Type: application/json');
    echo json_encode($response);
  }else
  {
    $response = array(
      'status' => '403',
      'message' => 'Invalid Access Key'
    );
    //http_response_code(403);
    header('Content-Type: application/json');
    echo json_encode($response);
  }
}else{
  $response = array(
    'status' => '403',
    'message' => 'Access Forbidden'
  );
  //http_response_code(403);
  header('Content-Type: application/json');
  echo json_encode($response);
}

Tapi tidak berhasil (tidak bisa membaca $_POST) : [img]https://xdrive.xyz/share/Screenshot%20from%202019-03-31%2009-21-15.png[/img]

Tapi...... Ketika saya ubah jadi method GET seperti ini :

$key = "CNtVA26CRRqdd293H5rdWq81Opa4UCi2GEYyulTV0zAjIytTDQ86vTvAExKqhU9P";
if(isset($_GET['key']))
{
  if ($_GET['key'] == $key)
  {
    $response = array(
      'status' => '200',
      'message' => 'Access Granted'
    );
    //http_response_code(200);
    header('Content-Type: application/json');
    echo json_encode($response);
  }else
  {
    $response = array(
      'status' => '403',
      'message' => 'Invalid Access Key'
    );
    //http_response_code(403);
    header('Content-Type: application/json');
    echo json_encode($response);
  }
}else{
  $response = array(
    'status' => '403',
    'message' => 'Access Forbidden'
  );
  //http_response_code(403);
  header('Content-Type: application/json');
  echo json_encode($response);
}

Hasilnya success, sesuai ekspetasi saya:

Itu, kira-kira masalahnya di mana gan?

avatar xdnroot
@xdnroot

44 Kontribusi 6 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

Jawaban Terpilih

FIXED!

Masalah : Saya menyimpan filenya di http://localhost/test/index.php Di atas saya menuliskan "http://localhost/test"

Cara Mengatasi : Penulisan Server API-nya harus: http://localhost/test/ atau http://localhost/test/index.php atau http://localhost/test/index (kalo seperti gini harus ada konfigurasi di .htaccess :v) <pre> RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^index index.php RewriteRule ^index/ index.php </pre>

Terimakasih.

avatar xdnroot
@xdnroot

44 Kontribusi 6 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban