【问题标题】:Endpoint can't make a POST request properly in PHP using SLIM framework端点无法使用 SLIM 框架在 PHP 中正确发出 POST 请求
【发布时间】:2018-12-14 10:15:47
【问题描述】:

我正在尝试使用遵循tutorial 的 SLIM 框架创建一个应该连接到数据库的 API 服务器。实际上我做了一个端点,从数据库中获取数据,但不能插入新的。

每次我尝试插入新数据时,POST 参数都是空值,并且数据库只发送一条错误消息。

在我的 groupes.php 中,我有以下内容:

<?php

use Slim\Http\Request;
use Slim\Http\Response;

// Routes

$app->get('/[{name}]', function (Request $request, Response $response, array$args) {
   // Sample log message
   $this->logger->info("Slim-Skeleton '/' route");

   // Render index view
   return $this->renderer->render($response, 'index.phtml', $args);
});

$app->group('/api', function () use ($app) {

   $app->group('/v1', function () use ($app) {
       $app->get('/clients', 'getClients');
       $app->post('/make', 'addClient');
   });
});

上面的代码只是定义了两个端点:getClientsaddClient

在我的 index.php 中我有:

function getConnection(){
    $dbhost = "127.0.0.1";
    $dbuser = "root";
    $dbpass = "dzpga883yRusPHhv";
    $dbname = "pruebaandroid";
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

//method: GET
//domain: mi-api/api/v1/clients
function getClients($response){
    $sql = "SELECT * FROM cliente";
    try{
        $stmt = getConnection()->query($sql);
        $client = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;

        return json_encode($client);
    } 

    catch(PDOException $e){
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

//method: POST
//domain: mi-api/api/v1/make
function addClient($request) {
    $client = json_decode($request->getBody());

    $sql = 'INSERT INTO cliente (nombre, apellido, cedula, direccion, telefono, email) VALUES (:nombre, :apellido, :cedula, :direccion, :telefono, :email)';

    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(":nombre", $client->nombre);
        $stmt->bindParam(":apellido", $client->apellido);
        $stmt->bindParam(":cedula", $client->cedula);
        $stmt->bindParam(":direccion", $client->direccion);
        $stmt->bindParam(":telefono", $client->telefono);
        $stmt->bindParam(":email", $client->email);
        $stmt->execute();
        $client->id = $db->lastInsertId();
        $db = null;
        echo json_encode($client);
    } 

    catch(PDOException $e){
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

因此,使用邮递员向mi-api/api/v1/clientes 发出 GET 请求,检索所有指定的数据,但是当我尝试使用 POST 请求mi-api/api/v1/make?nombre=asdf&amp;apellido=asdf&amp;cedula=asdf&amp;direccion=asdf&amp;telefono=asdf&amp;email=asdf 时,它应该插入新信息,但邮递员给了我以下错误:

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nombre' cannot be null}}

看起来addClient 函数没有获取参数,因此将其视为空值。为什么会这样?我不知道这里有什么问题,欢迎任何回复。

查看图片

PD:是的,DB 的格式很好,它有一个 PK 自动增量的 ID,我尝试打印 echo client-&gt;nombre 并且只打印错误。

【问题讨论】:

  • 您似乎正在发送带有 GET 参数的 POST 请求。参数用于请求正文中。

标签: php post get request slim


【解决方案1】:

您正在将参数添加为 GET 参数(添加在 url 上)。

使用 BODY -> form-data 选项卡指定 POST 参数(与 PARAMS 选项卡中的参数相同)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2020-05-23
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2017-10-15
    相关资源
    最近更新 更多