PHP 7中的Delete方法没有收到参数

时间:2017-07-04 09:29:41

标签: php angularjs rest ionic-framework

我们正在编写一个Restful API,可以从离子应用程序调用。我们使用以下代码从角度服务发送:

      .service('ServiceName', function($http) {
       return {
           borrarAlumno: function(par) {
               var link = "http://localhost/ws/index.php";
               return $http.delete(link, {
                   id: par.id
               })
           }
       }
   })

当我们尝试从php读取id参数时,我们什么都得不到。这是我们使用的代码:

    if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}


// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS");

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    exit(0);
}

$input = file_get_contents('php://input');
print_r($_SERVER['REQUEST_METHOD']);
exit();

我们尝试使用类似的代码来获取PUT,GET和POST方法的参数,并且它有效。

任何帮助都将不胜感激。

编辑:

这个php被称为另一个PHP:

class AlumnosAPI
{
    public function API()
    {
        header('Content-Type: application/JSON');
        $method = $_SERVER['REQUEST_METHOD'];

        switch ($method)
        {
          case 'GET':
              include('get.php');
              break;
          case 'POST':
              include('post.php');
              break;
          case 'PUT':
              include('put.php');
              break;
          case 'DELETE':
              include('delete.php');
              break;
          default:
              echo 'NOT SUPPORTED';
              break;
        }
    }

}

编辑2:delete.php的完整代码

  if (isset($_SERVER['HTTP_ORIGIN']))
  {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
  }


    // Access-Control headers are received during OPTIONS requests
  if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
  {

      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
          header("Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS");

      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
          header("Access-Control-Allow-Headers:{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

      exit(0);
  }

  $valor['borrado'] = 0;

  include('conexion.php');

  $id = "";

  //parse_str(file_get_contents('php://input'), $delete);
  $input = file_get_contents('php://input');
  print_r($input);
  exit();

  foreach (array_keys($delete) as $key)
  {
    $$key = $delete[$key];
  }

  if ($id != "")
  {
    //PREPARAMOS LA SENTENCIA SQL
    $sql = "DELETE FROM Alumnado WHERE `ID`=?";

    $resultado1=$conexion->prepare($sql);

    $resultado1->bind_param('i', $id);

    $resultado1->execute();

    $valor['borrado'] = $resultado1->affected_rows;
  }

  $datos=json_encode($valor);

  echo $datos;

1 个答案:

答案 0 :(得分:1)

Angular不会发送DELETE的请求正文,因此您必须从该网址读取ID。

.service('ServiceName', function($http) {
       return {
           borrarAlumno: function(par) {
               var link = "http://localhost/ws/index.php";
               return $http.delete(link, {
                   params : {id: par.id}
               })
           }
       }
   })

通过$_GET['id']代替file_get_contents('php://input');

获取ID