2014-04-07 67 views
0

我無法將數組轉換爲xml,然後將其作爲xml文章發佈到第三方URL。如何將數組轉換爲xml併發布到第三方?

我相信我很接近,但我失去了一些工作。 我正在使用WordPress和重力形式(我不認爲它很重要)

這是我到現在爲止。

function post_to_third_party($entry, $form) { 

    $post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx'; 
    $body = array('firstname' => $entry['8.3'],  
    'lastname' => $entry['8.6'], 
    'dayphone' => $entry['12'], 
    'email' => $entry['11']  
); 

    $xml = new SimpleXMLElement('<application/>'); 
    $body = array_flip($body); 
    array_walk($body, array ($xml, 'addChild')); 
    print $xml->asXML(); 

    $ch = curl_init($url); 
    //curl_setopt($ch, CURLOPT_MUTE, 1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $post_url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $output = curl_exec($ch); 
    echo $output; 

    curl_close($ch); 
} 

我也嘗試下面的代碼,這似乎工作,但後續代碼var_dump看起來像串(201)「$名字$姓氏$ dayphone $電子郵件」 OK

我不知道該如何填寫隨着數據的XML標籤從$體陣列

聚集在這裏是我用這個結果

add_action('gform_after_submission', 'post_to_third_party', 10, 2); 
function post_to_third_party($entry, $form) { 

$post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx'; 
$body = array(
    'firstname' =>  $entry['8.3'],  
    'lastname' =>  $entry['8.6'], 
    'dayphone' => $entry['12'], 
    'email' => $entry['11']  
    ); 
$xml = ' 

    <?xml version="1.0" encoding="UTF-8"?> 
    <application> 
    <firstname>$firstname</firstname> 
    <lastname>$lastname</lastname> 
    <dayphone>$dayphone</dayphone> 
    <email>$email</email> 
    </application>'; 
var_dump($xml); 

$ch = curl_init($url); 
    //curl_setopt($ch, CURLOPT_MUTE, 1); 
    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_URL, $post_url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    $output = curl_exec($ch); 
    echo $output; 

    curl_close($ch); 

} 
+0

的var_dump你的XML和張貼。我很確定你需要爲你的數組創建一個循環來生成XML。 –

+0

另外,有什麼問題?你的PHP不會創建一個XML文檔? XML文檔已創建但未發送? XML文檔被髮送但被其他服務器拒絕?一個可能的問題:您將SimpleXMLElement對象傳遞給'CURLOPT_POSTFIELDS'參數的'curl_setopt'函數 - 是否要傳遞XML字符串? – Kryten

+0

這就是我在做var_dump($ xml)時得到的結果; test(801)[email protected] object(SimpleXMLElement)#310(3){[「lastname」] => string(4)「test」[「dayphone」] => string(13)「(801 )735-2222「[」email「] => string(14)」[email protected]「}導入錯誤 –

回答

0

好的代碼,這裏的問題:

您在轉換爲XML之前用於構建數組的方法意外刪除了一些數據。

你有這樣的事情開始了:

$body = array(
    "firstname" => "Test", 
    "lastname" => "Test", 
    "dayphone" => "(801)735-2222", 
    "email" => "[email protected]", 
); 

然後調用array_flip,它給你這樣的:

$body = array(
    "Test" => "firstname", 
    "Test" => "lastname", 
    "(801)735-2222" => "dayphone", 
    "[email protected]" => "email", 
); 

現在你已經把陣列中的兩個「試驗」鍵。 PHP保持最後一個拋棄第一個。所以你的XML文檔現在缺少一個必需的鍵:「firstname」。

的解決方案是構建XML文檔的老式方法和遠離花哨的東西走像array_flip

foreach($body as $k=$v) 
    $xml->addChild($k, $v);