简体   繁体   English

PHP收发数据

[英]PHP receives and sends data

I have the following php code, it sends the data to a leads tool.我有以下 php 代码,它将数据发送到线索工具。

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mkt.university-private.internal/form/submit",                                                                                                                                                                                                                                                                                                                                                                           CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => array('mauticform[f_email]' => 'gbsilva@gmail.com','mauticform[f_name]' => 'Gabriel','mauticform[formId]' => '5'),
  CURLOPT_HTTPHEADER => array(
    "X-Forwarded-For: 91.92.103.192"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

The problem is that I have to enter the data manually in the php script.问题是我必须在 php 脚本中手动输入数据。

I now have an CRM that does a POST and sends the following data in body form-data:我现在有一个 CRM,它执行 POST 并以正文形式数据发送以下数据:

email=gbsilva@40gmail.com&name=Gabriel&IP=91.92.103.192&formId=5

what i need is that my php code accepts to receive an CRM post with these values above and make the request in my leads tool with the data that came from CRM.我需要的是我的 php 代码接受接收具有上述这些值的 CRM 帖子,并使用来自 CRM 的数据在我的潜在客户工具中提出请求。

on my erp, i can call a url and i will call the url of my script.php在我的 erp 上,我可以调用 url,我将调用我的脚本的 url。php

he needs before sending convert and put the name of the fields between this nomenclature, the leads tool only accepts fields with this nomenclature:他需要在发送转换之前将字段名称放在此命名法之间,潜在客户工具仅接受具有此命名法的字段:

mauticform[f_FIELDNAME]

Can anyone help谁能帮忙

Very easy just need to utilize $_POST to transfer values to variables and then use it非常简单,只需要利用$_POST将值传输到变量然后使用它

<?php

$P_email = $_POST['email'];
$P_name = $_POST['name'];
$P_formId = $_POST['formId'];
$P_ip = $_POST['IP'];

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mkt.university-private.internal/form/submit",   
  //..hidden                                                                                                                                                                                                                   
  CURLOPT_POSTFIELDS => array('mauticform[f_email]' => $P_email,'mauticform[f_name]' => $P_name,'mauticform[formId]' => $P_formId),

  //hidden

  //*update* FOR IP
  CURLOPT_HTTPHEADER => array(
    "X-Forwarded-For: $P_ip"
  ),
));


//..

UPDATE : So to address the dynamic variable names更新:所以要解决动态变量名称

//Create an array to hold the name=value pairs
$P_arr = [];
//Loop over $_POST and populate $P_arr
foreach($_POST as $key=>$value){
    $P_arr[$key] = $value; 
   // $key will run through all those keys' values you sent //name ,email ..
   // so will $value but on the literals like "gbsilva@40gmail.com", "Gabriel"
}

/* We have now an array of key value pairs */
// adjust the KEYs to "mauticform"'s format before using
$mauticformArr = [];

foreach($P_arr as $key=>$value){
   if($key != 'IP'){
      if($key!= 'formId')
        $mauticformArr['mauticform[f_'.$key .']'] = $value;
      else
        $mauticformArr['mauticform['.$key .']'] = $value;       
   }
}

// Then use inside you code as
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mkt.university-private.internal/form/submit",   
  //..hidden                
CURLOPT_POSTFIELDS => $mauticformArr,
  //..hidden 
  //..

You may find that "Guzzle" works better than actually using CURL...您可能会发现“Guzzle”比实际使用 CURL 效果更好......

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

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