如何通过POST方法将XML数据发送到供应商服务器?

时间:2018-08-15 12:56:20

标签: json xml post

我有一个无法解决的问题,因为我本人实际上没有编程背景。我和我的朋友们经营着几家网上商店,我们也正在开设一家网上商店(WooCommerce平台顺便说一句),在那里我们将进行直销。客户订购产品时,需要将其自动发送给供应商。

<?php

$email = 'Mail';
$apikey = 'APICode';


$apiurl = 'Where the XML will be send to';


$xml = '<?xml version="1.0"?>
<orderdetails>
<customerdetails>
    <email>MyEmail</email>
    <apikey>MyAPI</apikey>
    <output>advanced</output>
</customerdetails>
<receiver>
    <name>Name</name>
    <street>Straatnaam</street>
    <house_nr>24</house_nr>
    <postalcode>Postal</postalcode>
    <city>City</city>
    <country>Countrycode</country>
    <phone>+30000000</phone>
</receiver>
<products>
    <artnr>Productcode</artnr>
</products>
</orderdetails>';

// Check whether the config vars are all set
if(empty($email) || empty($password)){
    die('Please enter your config vars');
}

// Check whether the cURL module has been installed
if(!function_exists('curl_init')){
    die('You do not have the cURL functions installed! Ask your host for more info.');
} else {

    // Send the XML request
    $postfields = 'data='.$xml;
    $ch = curl_init($apiurl);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$postfields);
    $result = curl_exec($ch);
    curl_close($ch);

    if($ch === false || $result === false){
        die('There was a problem with the connection to EDC');
    } else {
        $json = json_decode($result,true);

        // Success
        if($json['result'] == 'OK'){

            echo '<pre>';
            echo 'The order was successful. The following output was received from EDC:'.PHP_EOL;
            print_r($json);
            echo '</pre>';

        // Failure
        } else {
            echo '<pre>';
            echo 'There was a problem with the order request. The following output was received from EDC:'.PHP_EOL;
            print_r($json);
            echo '</pre>';
        }
    }
}
?>

这是我目前的代码。这是在客户下订单后将XML发送给供应商的代码吗?我如何在商店中使用它。因此,在某人订购后,需要制作一个XML,该XML将激活该脚本(??)以将该XML发送给我的供应商。

我希望有人能帮助我,如果有任何问题,我会尽力回答。

最诚挚的问候,

丹尼斯

0 个答案:

没有答案
相关问题