简体   繁体   English

PHP发送和接收数据curl或json

[英]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'. 'output_file.php'将数据从域'a'发送到位于域'b'的input_file。 Data from output file will later be send to crm via api. 来自输出文件的数据稍后将通过api发送到crm。

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: 这是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: 这是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. 请从您的输出文件中删除POST函数,它是未经修改的且无用。

$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. 在使用POST发送数据时,您将需要在目标站点上捕获POST。 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: 在使用$ _POST变量时,您可能需要研究一下安全性,以确保接收到的数据不会对您造成伤害:

PHP $_GET security, $_POST security best practice PHP $ _GET安全性,$ _ POST安全性最佳做法

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

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

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