如何在curl中传递XML

时间:2011-08-31 09:04:54

标签: xml

我想使用curl发布XML数据。我正在使用这些代码行:

            $data = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
    $data.= "<contacts>\r\n";
    $data.= "<contact>\r\n";
    $data.= "<firstName>$firstName</firstName>\r\n";
    $data.= "<lastName>$lastName</lastName>\r\n";
    $data.= "<email>$Email_Address</email>\r\n";
    $data.= "<status>normal</status>\r\n";
    $data.= "<contact>\r\n";
    $data.= "</contacts>\r\n";

    $url =  "https://app.sandbox.icontact.com/icp/a/".$account_id."/c/".$client_folder_id."/contacts";
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_POST, true);
        curl_setopt($handle, CURLOPT_POSTFIELDS,$data);
        $response = curl_exec($handle);

当我收到回复时,它会显示此消息:

           <?xml version="1.0" encoding="UTF-8"?>
           <response>
       <errors>
    <error>The XML that was passed in your request was malformed. Please provide valid XML.</error>
       </errors>
           </response>

我需要在XML输入中更改内容。

由于

1 个答案:

答案 0 :(得分:0)

它通常是一个问题,你需要在发送之前对xml进行url编码。试试$data = urlencode($data)

这将为您提供%3C?xml version="1.0" encoding="UTF-8"?%3E等输出