使用php以编程方式传递soap值

时间:2019-04-24 10:38:26

标签: php curl soap

我有以下php脚本来更新xml值:

//The XML string that you want to send.
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<reminder>
    <date>2019-20-02T10:45:00</date>
    <heading>Meeting</heading>
    <body>Team meeting in Boardroom 2A.</body>
</reminder>';


//The URL that you want to send your XML to.
$url = 'http://localhost/xml';

//Initiate cURL
$curl = curl_init($url);

//Set the Content-Type to text/xml.
curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));

//Set CURLOPT_POST to true to send a POST request.
curl_setopt($curl, CURLOPT_POST, true);

//Attach the XML string to the body of our request.
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);

//Tell cURL that we want the response to be returned as
//a string instead of being dumped to the output.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//Execute the POST request and send our XML.
$result = curl_exec($curl);

//Do some basic error checking.
if(curl_errno($curl)){
    throw new Exception(curl_error($curl));
}

//Close the cURL handle.
curl_close($curl);

//Print out the response output.
echo $result;

但是,我打算做的是,我想使用动态值制作$ xml,已经尝试过这样的事情:

$date= $_POST['datevalue'];
$heading= $_POST['meetingvalue'];
$body= $_POST['bodycontent'];

$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<reminder>
    <date>{$date}</date>
    <heading>{$date}</heading>
    <body>{$date}</body>
</reminder>';

不幸的是,以上代码无法正常工作,看来{$ date}并没有发送任何东西来休息SOAP。

任何人都有解决此问题的经验,

2 个答案:

答案 0 :(得分:0)

我不是这方面的专家,所以给个建议, 尝试使用双引号,如下所示:

$xml = "<?xml version='1.0' encoding='ISO-8859-1'?>
<reminder>
  <date>{$date}</date>
  <heading>{$date}</heading>
  <body>{$date}</body>
</reminder>";

希望有帮助

答案 1 :(得分:0)

如何替换字符串?

$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
    <reminder>
        <date>%s</date>
        <heading>%s</heading>
        <body>%s</body>
    </reminder>';

$message = sprintf($xml, $date, $heading, $body);

无论如何,完整的动态XML方法可以使用DomDocument类。

$doc = new DomDocument('1.0', 'iso-8859-1');
$reminder = $doc->createElement('reminder');

$date = $doc->createElement('date', $dateValue);
$reminder->appendChild($date);

$heading = $doc->createElement('heading', $headingValue);
$reminder->appendChild($heading);

$body = $doc->createElement('body', $bodyValue);
$reminder->appendChild($body);

$doc->appendChild($reminder);
$xmlString = $doc->saveXML();

$xmlString变量将您的xml树包含为具有动态值的字符串。