0

I'm trying to make two file talk to each other. 'output_file.php' to send data from domain 'a' to input_file located on domain 'b'. Data from output file will later be send to crm via api.

I'm stuck as I don't know what am I doing wrong, what should I change in these files?

Here is output_file.php:

<?php
    //send cURL
    $curl = 'https://domain_name/input.php';
    $fields = array(
        'name' => urlencode($_POST['name']),
        'email' => urlencode($_POST['email']),
        'tel' => urlencode($_POST['tel']),
    );
    //var_dump($fields);
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    //var_dump($fields_string);
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $curl);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch);
    //var_dump($result);
    curl_close($ch);*/
?>

Here is input_file.php:

 // main data about the person. person_id is added later dynamically - PERSON DATA
$person = array(
 'name' => 'name from output_file.php',
 'email' => 'email from output_file.php',
 'phone' => 'tel from output_file.php'
);
ludi
  • 1
  • 2
  • As you do use POST to send data from output_file.php to input_file.php you will have to use $_POST in your input_file.php to get your values. – S.Gartmeier Sep 25 '17 at 08:20
  • Thank you nathariel, could you please give an example if possible? – ludi Sep 25 '17 at 08:28

2 Answers2

0

You can use below snippet for this. It should be work. Ps. Please delete POST functions from your output file, it's uncesseray and useless.

$person = array(
 'name' => $_REQUEST['name'],
 'email' => $_REQUEST['email'],
 'phone' => $_REQUEST['phone'],
);

Best,

Gokturk
  • 47
  • 1
  • 7
0

As you do use POST to send your data, you will need to capture the POST on the target site. As you do use $_POST variables, you may want to have a look into security, to make sure the data recieved can not harm you:

PHP $_GET security, $_POST security best practice

Your Outfile:

<?php

    $curl = 'https://domain_name/input.php';
    $fields = array(
        'name' => urlencode($_POST['name']),
        'email' => urlencode($_POST['email']),
        'tel' => urlencode($_POST['tel']),
    );

    // here you do prepare your POST data
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $curl);

    // here you define that your data will be sent via POST
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    // this curlopt ensures the output of your destination is captured
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
?>

Your Input/Destination file:

<?php
// user $_POST to populate your array
$person = array(
 'name' => $_POST['name'],
 'email' => $_POST['email'],
 'phone' => $_POST['tel']
);
// see the result
var_dump($person);
?>
S.Gartmeier
  • 476
  • 5
  • 14