我正在尝试发送数据,但每次都会失败。
当我从Angular将其发送到cURL时,Smehow会显示未定义索引(我知道该变量很可能为空或未发送)
inicio.ts :
insertar() {
this.fname = this.Form.value.fname;
this.email = this.Form.value.email;
this.http.post('http://localhost/curl/config/config.php', {
fname : this.fname,
email: this.email,
}).subscribe((data: any) => {
console.log(data);
this.router.navigate(['/inicio']);
}, error => {
console.log(JSON.stringify(error));
});
}
config.php
<?php
# An HTTP GET request example
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-
Token');
header('Content-Type: application/x-www-form-urlencoded');
$method = $_SERVER['REQUEST_METHOD'];
$url = 'http://localhost/curl/api/rest.php';
$postdata = file_get_contents("php://input");
$data = json_decode($postdata, true);
print_r($data);
if(isset($_POST['fname']) && $_POST['email']){
$data = [
"fname" => $_POST['fname'],
"email" => $_POST['email'],
];
}
if(isset($_POST['id'])){
$id = [
"id" => $_POST['id']];
}
switch ($method) {
case 'GET':
// not ready
break;
case 'POST':
postData($url,$method,$data);
break;
case 'PUT':
# code...
break;
case "DELETE":
// not ready
break;
default:
//echo json_encode(['Error'=>'Un Error ha ocurrido']);
break;
}
function postData($url,$method,$data){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
if(!$response){
return false;
} else {
print_r($response);
}
curl_close($ch);
}
所以我的php文件中缺少什么?因为我认为有些东西会停止或无法识别我发送的数据。
编辑:如果我使用终端并执行
curl -X POST <url> -d param:value -d param2:value2
有效
第二次修改: 现在它可以在config.php文件中工作,但是当我尝试从$ data发送信息时,它发送的内容为空。
API代码:
<?php
header('content-type:application/json');
header('Access-Control-Allow-Origin: *');
include ('../config/conn.php');
$method = $_SERVER['REQUEST_METHOD'];
$postdata = file_get_contents("php://input");
switch ($method) {
case 'GET':
get($conexion);
break;
case 'POST':
post($conexion,$postdata);
break;
case 'PUT':
put();
break;
case 'DELETE':
delete($conexion,$postdata);
break;
default:
# code...
break;
}
function get($conexion){
$sql = 'SELECT * FROM tbl_users';
$resultado = pg_query($conexion,$sql);
while ($rows = pg_fetch_array($resultado)) {
$array[] = $rows;
}
echo json_encode($array);
}
function post($conexion,$postdata){
//It displays as empty string
echo $postdata;
/*
$sql = "INSERT INTO tbl_users(fname,email) VALUES('".$fname."','".$email."')";
$resultado = pg_query($conexion,$sql);
close($conexion);
*/
}
function put(){
}
function delete($conexion,$postdata){
parse_str($postdata, $arr);
$idn = $arr['id'];
$id = (int)$idn;
echo $id;
}
function close($conexion){
pg_close($conexion);
}
?>
答案 0 :(得分:0)
发布json内容时,不能使用$_POST
变量
尝试这样的事情
$jsonBody= file_get_contents('php://input');
$data = json_decode($jsonBody, true);
$data
应该是具有fname
和email
键的关联数组