2017-09-25 89 views
0

我想讓兩個文件相互交談。 'output_file.php'將數據從域'a'發送到位於域'b'的輸入文件。 輸出文件中的數據稍後將通過api發送至crm。PHP發送和接收數據curl或json

我被卡住了,因爲我不知道我在做什麼錯,我應該在這些文件中更改什麼?

這裏是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);*/ 
?> 

這裏是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' 
); 
+0

當你使用POST從output_file.php數據發送到input_file.php你將不得不使用$ _ POST在input_file.php讓你的價值觀。 – natheriel

+0

謝謝nathariel,如果可能,請舉個例子嗎? – ludi

回答

0

您可以使用以下代碼段用於此。它應該是工作。 Ps。請從您的輸出文件中刪除POST函數,它的內容很簡單而且沒用。

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

最佳,

0

當你使用POST發送你的數據,你會需要捕獲POST目標站點上。當你這樣做使用$ _ POST變量,你可能想看看到安全性,以確保收到不能傷害你的數據:

PHP $_GET security, $_POST security best practice

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

你輸入/目標文件:

<?php 
// user $_POST to populate your array 
$person = array(
'name' => $_POST['name'], 
'email' => $_POST['email'], 
'phone' => $_POST['tel'] 
); 
// see the result 
var_dump($person); 
?> 
+0

謝謝你natheriel。這工作很棒:) – ludi