[英]How can I see what is inside the $request variable in a Slimapp php POST?
本质上,我正在寻找故障排除帮助。 这在上周有效,我什么也没做。
我的本地计算机上运行着Apache,并且正在使用Chrome插件REST控制台将POST发送到我自己的URL。 正如我提到的,上周效果很好。 本周,我收到一个错误消息,似乎表明我的JSON对象中包含null或空值。 错误消息是:
{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'id' cannot be null}id was:
我的代码:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
function connect_to_db() {
$conn = new PDO('mysql:host=localhost;dbname=moviedb','movieuser','ONVYyc5FiBPcKaK4');
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $conn;
}
$app = new \Slim\App;
$app->post('/movies/add', function (Request $request, Response $response) {
$id = $request->getParam('id');
$name = $request->getParam('name');
$description = $request->getParam('description');
$stars = $request->getParam('stars');
$length = $request->getParam('length');
$image = $request->getParam('image');
$year = $request->getParam('year');
$rating = $request->getParam('rating');
$director = $request->getParam('director');
$url = $request->getParam('url');
$sql_query = "INSERT INTO movies (id,name,description,stars,length,image,year,rating,director,url) VALUES (:id,:name,:description,:stars,:length,:image,:year,:rating,:director,:url)";
try{
$db = connect_to_db();
$stmt = $db->prepare($sql_query);
$stmt->bindParam(':id',$id);
$stmt->bindParam(':name',$name);
$stmt->bindParam(':description',$description);
$stmt->bindParam(':stars',$stars);
$stmt->bindParam(':length',$length);
$stmt->bindParam(':image',$image);
$stmt->bindParam(':year',$year);
$stmt->bindParam(':rating',$rating);
$stmt->bindParam(':director',$director);
$stmt->bindParam(':url',$url);
$stmt->execute();
$db = null;
echo '{"Result":{"text":"Added"}';
}
catch (PDOException $e) {
echo '{"error":{"text":'.$e->getMessage().'}'.'id was: '.$id;
}
});
$app->run();
和我用来测试的JSON:
{"id":"test",
"name":"test movie",
"description":"test movie",
"stars":"test movie",
"length":"test movie",
"image":"test movie",
"year":"test movie",
"rating":"test movie",
"director":"test movie",
"url":"test movie"
}
我该怎么做才能解决此问题? 在POST和我的apache服务器之间,然后在可能剥离JSON的php之间发生了什么? 如果$ request变量中有JSON,我怎么看? 可能已更改了什么配置,这将导致该配置停止工作?
在Android应用程序和REST控制台中,手动将content-type
设置为application/json
可以达到目的。 我仍然不确定为什么以前没有设置该参数就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.