在XML中回显PHP变量并将值发布到webservice

时间:2018-01-12 06:15:17

标签: php xml

我需要在基于SOAP的Web服务上发布PHP变量值,以及我无法在XML元素中转换PHP POST值。

如何在abc元素中获取sName的POST变量?

我的PHP代码

    <?php

    //session_start();

    $server_name= abc;

   $xml_post_string = '<?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>
  <createtest xmlns="http://tempuri.org/">
  <sA>ADD</sA>
  <sType>service</sType>
  <sName>"$server_name"</sName>
  </createtest>
  </soap:Body>
  </soap:Envelope>';


     $headers = array(
     "Content-Type: text/xml; charset=utf-8",
     "Host: http://example.com",
     "Content-length: ".strlen($xml_post_string),
     "SOAPAction: http://tempuri.org/createtest"
      );

    url = 'http://example.com';  // WSDL web service url for request 
    method/function
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP 
    request
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     echo $response = curl_exec($ch);
     ?>

1 个答案:

答案 0 :(得分:0)

当您将变量定义为字符串时,必须在abc周围加上引号:

$server_name = "abc";

您还可以将变量放在字符串中,以便更容易将其理解为这样一个长字符串中的变量:

   $xml_post_string = '<?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>
  <createtest xmlns="http://tempuri.org/">
  <sA>ADD</sA>
  <sType>service</sType>
  <sName>'.$server_name.'</sName>
  </createtest>
  </soap:Body>
  </soap:Envelope>';