将值从 ionic 传递到 php curl

我正在尝试学习curl。我需要将变量从 Ionic 传递到 PHP 文件。


这是到目前为止的代码,我想编写传递的变量而不是硬编码值"Saurabh\"


Ionic / Angular .ts 代码


  create_person_name(){

  console.log(this.val1);

  console.log('inside function');

  let empname= this.val1;

 //let msg = "Thank you for trusting KRANTI PRODUCTS. Your Complaint Number is: " + this.ticketnumber;

  let params = '&empname='+this.val1;

  var headers = new Headers();

  headers.append('Content-Type', 'application/x-www-form-urlencoded');

  return this._http.post("https://example.com/" + "create-person-name.php", params, {headers: headers})

  .map((response: Response) => response.json());

  }

PHP


我该如何提及&empname来代替"Saurabh\"?


<?php


$curl = curl_init();


curl_setopt_array($curl, array(

  CURLOPT_URL => "https://centralindia.api.cognitive.microsoft.com/face/v1.0/persongroups/2/persons",

  CURLOPT_RETURNTRANSFER => true,

  CURLOPT_ENCODING => "",

  CURLOPT_MAXREDIRS => 10,

  CURLOPT_TIMEOUT => 30,

  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

  CURLOPT_CUSTOMREQUEST => "POST",

  CURLOPT_POSTFIELDS => "{  \r\n    \"name\" : \"Saurabh\",  \r\n    \"userData\" : \"TCO\" ,\r\n    \"recognitionModel\": \"recognition_02\"\r\n}",


慕虎7371278
浏览 95回答 1
1回答

Qyouu

像这样更新帖子字段..."{&nbsp; \r\n&nbsp; &nbsp; \"name\" : \"{$_POST['empname']}\",&nbsp; \r\n&nbsp; &nbsp; \"userData\" : \"TCO\" ,\r\n&nbsp; &nbsp; \"recognitionModel\": \"recognition_02\"\r\n}",您可以使用数组构建它,然后对其进行 json_encode...$fields = array(&nbsp; &nbsp; 'name' => $_POST['empname'],&nbsp; &nbsp; 'userData' => 'TCO',&nbsp; &nbsp; 'recognitionModel' => 'recognition_02',);然后更新curl数组CURLOPT_POSTFIELDS => json_encode($fields)
打开App,查看更多内容
随时随地看视频慕课网APP