2015-04-17 118 views
1

當前以HTTP Post格式發佈我的數據,我需要將其發佈爲XML並添加標題。使用Curl以XML格式發佈數據

這是我目前發佈我的數據的方式。

//extract data from the post 
extract($_POST); 

//set POST variables 
$url = 'https://test.com/checklead'; 
$fields = array(
         'surname' => urlencode($surname), 
         'first_name' => urlencode($first_name), 
         'dob' => urlencode($dob), 
         'email' => urlencode($email), 
         'home_phone' => urlencode($home_phone), 
         'mobile_phone' => urlencode($mobile_phone), 
         'work_phone' => urlencode($work_phone), 
         'postcode' => urlencode($postcode), 
         'leadid' => urlencode(123), 
         'affid' => urlencode(123), 
         'subid' => urlencode(123), 
         'bank_acno' => urlencode($bank_acno), 
         'bank_sort' => urlencode($bank_sort), 
         'amount_required' => urlencode($amount_required), 

       ); 

//url-ify the data for the POST 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
rtrim($fields_string, '&'); 

//open connection 
$ch = curl_init(); 

//set the url, number of POST vars, POST data 
curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, count($fields)); 
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); 

//execute post 
$result = curl_exec($ch); 

//close connection 
curl_close($ch); 

我如何使用這種XML格式,並添加頁眉:http://www.header.com

回答

0

您還沒有設置內容類型。 嘗試沿線的東西:

curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Header1: my_header_value' 
    )); 
$output = curl_exec($ch); 

,或者你可以看看使用庫如狂飲它做了很多錯誤處理等你的。