2016-09-14 137 views
0

我有一個PHP代碼發送一個帶有XML對象的POST請求到服務器,但它不起作用,我認爲問題與請求,我查找這樣的問題和我試圖做他們提出的建議,但仍然行不通。 這裏是我的PHP代碼:在PHP中發送XML對象請求

$url = 'http://example/service.asmx'; 
$xml='<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
<Get_schadual xmlns="http://tempuri.org/"> 
<User>exmaple</User> 
</Get_schadual> 
</soap:Body> 
</soap:Envelope>'; 
$post_data = array('xml' => $xml); 
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n X-Requested-With: XmlHttpRequest", 
'method' => 'POST', 
'content' => http_build_query($post_data))); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 
var_dump($result); 
+0

請指定你的意思是「它不工作」。你收到了什麼錯誤或你會得到什麼迴應? –

回答

0

我建議你使用cURL。下面的例子應該完成你想要做的事情:

$xml = "<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
<Get_schadual xmlns="http://tempuri.org/"> 
<User>exmaple</User> 
</Get_schadual> 
</soap:Body> 
</soap:Envelope>"; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "http://example/service.asmx"); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

$result = curl_exec($ch); 
curl_close($ch); 

var_dump($result);