在Curl PHP中发送XML

时间:2015-06-10 08:35:03

标签: php xml curl

我目前以普通邮寄方式发送表单数据。 我想以XML格式发送它。

当前格式:name = max 格式我需要:max

这是我目前的代码:

// This is the data to POST to the form. The KEY of the array is the name of the field. The value is the value posted.
$data_to_post = array();
$data_to_post['title'] = '$title';
$data_to_post['first_name'] = '$first_name';
$data_to_post['surname'] = '$surname';
$data_to_post['email'] = '$email';
$data_to_post['dob'] = '$dob';

// Initialize cURL
$curl = curl_init();

// Set the options
curl_setopt($curl,CURLOPT_URL, $form_url);

// This sets the number of fields to post
curl_setopt($curl,CURLOPT_POST, sizeof($data_to_post));

// This is the fields to post in the form of an array.
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);

//execute the post
$result = curl_exec($curl);

//close the connection
curl_close($curl);

?>

2 个答案:

答案 0 :(得分:1)

首先,您必须将数组转换为xml数据。将数组转换为xml array to xml conversion

的Reffer链接

现在您必须使用以下代码发送数据

$input_xml = ''; //XML Data 
$url=''; // URL


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "xmlRequest=" . $input_xml);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $data = curl_exec($ch);
    curl_close($ch);

答案 1 :(得分:0)

您无法发送XML。您必须在POST var

中发送XML字符串
$data_to_post = array();
$data_to_post['xml'] = '<?xml version="1.0"?>
<data_to_post>
<title><![CDATA['.$title']]><title>
<first_name><![CDATA['.$first_name']]><first_name>
<surname><![CDATA['.$surname']]><surname>
<email><![CDATA['.$email']]><email>
<dob><![CDATA['.$dob']]><dob>
</data_to_post>'