2016-09-14 113 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);