2013-01-07 33 views
0

我已經建立了一個使用php的自定義API,它是一個簡單的API,通過發佈XML數據。 ,我的工作張貼到API的代碼是:發送回覆 - PHP API

<?php 
$xml_data = '<document> 
<first>'.$first.'</first> 
<last>'.$last.'</last> 
<email>'.$email.'</email> 
<phone>'.$phone.'</phone> 
<body>TEST</body> 
</document>'; 
     $URL = "url"; 
     $ch = curl_init($URL); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $output = curl_exec($ch); 
     curl_close($ch); 
     $Response = curl_exec($ch);  
    curl_close($ch); 
    echo "Responce= ".$responce; 
?> 

在另一邊,上面代碼中的職位,以:

<?php 
$postdata = file_get_contents("php://input"); 
$xml = simplexml_load_string($postdata); 
$first = $xml->first; 
$last = $xml->last; 
$email = $xml->email; 
$phone = $xml->phone; 
?> 

然後我把這些PHP變量和發送到數據庫..所有這個代碼工作!

但我的問題是:如何將回復發送回發帖方? 如何使用curl_init發送到curl_exec?

任何幫助將是偉大的!謝謝 傑森

+1

你爲什麼要叫'curl_exec'兩次退回? – Madbreaks

+0

錯誤,對不起!進出口和關閉,離開反應和關閉。另一個錯誤是 – user1789437

回答

1

要返回你會做同樣的,就像對其他任何內容的響應,設置標題和回聲的輸出。例如,要返回XML響應,從腳本處理後的數據做如下

<?php 
$postdata = file_get_contents("php://input"); 
$xml = simplexml_load_string($postdata); 
$first = $xml->first; 
$last = $xml->last; 
$email = $xml->email; 
$phone = $xml->phone; 

// do your db stuff 

// format response 
$response = '<response> 
    <success>Hello World</success> 
</response>'; 
// set header 
header('Content-type: text/xml'); 
// echo xml identifier and response back 
echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62); 
echo $response; 
exit; 
?> 

你應該看到響應來自curl_exec()

+0

這工作100%! – user1789437