繁体   English   中英

尝试使用Angular通过POST到cURL发送数据

[英]Trying to send data via POST using Angular to cURL

我正在尝试发送数据,但是每次都会失败。
当我从Angular将其发送到cURL时,Smehow会显示未定义索引(我知道该变量很可能为空或未发送)

提示

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);
}


 ?>

postign json内容时,不能使用$_POST变量

试试这个

$jsonBody= file_get_contents('php://input');
$data = json_decode($jsonBody, true); 

$data应该是具有fnameemail键的关联数组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM