简体   繁体   中英

PHP send and receive data curl or json

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

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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