删除iOS应用程序API项(API调用调用PHP)


Delete Items API for iOS App (calls to PHP for the API call)

这是我们当前的设置。我们有一个iOS应用程序,它对我的PHP脚本进行API调用,该脚本通过PDO和MySQL处理请求和查询数据库。因此,在这种情况下,有一个update_items.php API,iOS应用程序会向其发送参数值,并根据用户是更新项目还是删除项目,API会相应地处理它,并多次查询数据库(全部在一个请求中)。

这是我的困境。我有更新位工作,但我如何使用相同的更新API通过POST请求删除项目?我的iOS开发者想出了一个快速的补救办法,如果用户滑动删除某个项目,他会将该项目的名称发送为"delete"或类似的内容。这将启动数据库记录的删除查询。我不喜欢这样,因为任何人都可以弄清楚这一点并利用这个系统。例如,在编辑项目时,我所要做的就是在DELETE中输入该项目的名称,API将以与删除请求相同的方式处理该项目。必须有更好的方法,如果有任何建议,我将不胜感激。下面是我当前处理API调用的PHP代码。然而,我的建议是,在用户单击DONE编辑他们的项目页面后,同时发送两个API调用。如果用户更新项目,则一个用于update.php;如果用户决定删除项目,则另一个用于delete.php。

// now check for updating/deleting ingredients for the menu item 
if( isset($the_request['id']) ) {
    /* 
        iterate through avalialable values because there could be multiple ingredient ids involved. handle it. 
    */
    for( $i=0;$i<count($the_request['id']);$i++ ) {
        // the queries. check if ingredient is being deleted or not via passed paramater value
        switch($the_request['name'][$i]) {
            case 'DELETE':
                // assign passed parameter for delete query
                $params = array(
                    ':id' => $the_request['id'][$i]
                );
                // the query
                $query = 'DELETE FROM TABLE WHERE id = :id';
            break;
            default:
                // assign passed parameters for query
                $params = array(
                    ':name'  => $the_request['name'][$i],
                    ':price' => $the_request['price'][$i]
                );
                // Remove the empty values
                $params = array_filter($params, function($param) { return !empty($param); });
                // Build an array of SET parameters
                $set = array_map(function($key) {
                    return sprintf('%s = %s', substr($key, 1), $key);
                }, array_keys($params));
                // don't forget the id
                $params[':id'] = $the_request['id'][$i];
                // the query
                $query = sprintf('UPDATE TABLE SET %s WHERE id = :id', implode(', ', $set));
        }
        // prepare statement
        if( $ingStmt = $dbh->prepare($query) ) {
            $ingStmt->execute($params);
        } else {
            echo json_encode(array('error' => $dbh->errorInfo().__LINE__));
        }
    }
    $ingStmt->closeCursor();
}
REST的答案是不要使用POST请求,使用单独的DELETE请求。