paquino
paquino

Reputation: 45

PHP receives and sends data

I have the following php code, it sends the data to a leads tool.

<?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]' => '[email protected]','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.

I now have an CRM that does a POST and sends the following data in body form-data:

[email protected]&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.

on my erp, i can call a url and i will call the url of my script.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

Upvotes: 0

Views: 57

Answers (2)

Vinay
Vinay

Reputation: 7674

Very easy just need to utilize $_POST to transfer values to variables and then use it

<?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 "[email protected]", "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 
  //..

Upvotes: 1

Mike Robinson
Mike Robinson

Reputation: 8995

You may find that "Guzzle" works better than actually using CURL ...

Upvotes: 0

Related Questions