PHP变量转换为XML

时间:2014-04-02 09:13:40

标签: php xml variables

请帮助我,例如我有下一个:

<php?
$phone = "7777111111";
$src = '<?xml version="1.0" encoding="UTF-8"?>    
<SMS> 
<sender>Blabla</sender>    
<text>Hello</text>   
</message>    
<numbers> 
<number messageID="msg11"> HERE MUST BE NUMBER!!!</number> 
</numbers>   
</SMS>;

$Curl = curl_init();    
$CurlOptions = array(   
CURLOPT_URL=>'http://atompark.com/members/sms/xml.php',  
CURLOPT_FOLLOWLOCATION=>false,   
CURLOPT_POST=>true,  
CURLOPT_HEADER=>false,   
CURLOPT_RETURNTRANSFER=>true,    
CURLOPT_CONNECTTIMEOUT=>15,  
CURLOPT_TIMEOUT=>100,    
CURLOPT_POSTFIELDS=>array('XML'=>$src),   
);  
?>

如何将变量$ phone放入XML中的标签中? 非常感谢

2 个答案:

答案 0 :(得分:3)

你的代码错了。

  • 您即将结束&#34; </message>&#34;没有打开。
  • 您必须使用&#39;关闭$ src字符串。之前;
  • PHP开放代码为<?php而不是<php?

您也可以使用DOMDocument对象轻松创建这种XML。

关于你的问题,字符串连接:

$name = "Foo";
$welcome = "Hello ".$name;
echo($welcome); // will output "Hello Foo"

所以,正确的代码是:

<?php
    $phone = "7777111111";
    $src = '<?xml version="1.0" encoding="UTF-8"?>    
<SMS>
  <message>
    <sender>Blabla</sender>    
    <text>Hello</text>   
  </message>    
  <numbers> 
    <number messageID="msg11">'.$phone.'</number> 
  </numbers>   
</SMS>';

    $Curl = curl_init();    
    $CurlOptions = array(   
        CURLOPT_URL=>'http://atompark.com/members/sms/xml.php',  
        CURLOPT_FOLLOWLOCATION=>false,   
        CURLOPT_POST=>true,  
        CURLOPT_HEADER=>false,   
        CURLOPT_RETURNTRANSFER=>true,    
        CURLOPT_CONNECTTIMEOUT=>15,  
        CURLOPT_TIMEOUT=>100,    
        CURLOPT_POSTFIELDS=>array('XML'=>$src),   
    ); 

阿波罗

答案 1 :(得分:1)

你可以用谷歌搜索,或者你可以花点时间阅读一些very basic introduction to PHP

<?php
$phone = "7777111111";
$src = '<?xml version="1.0" encoding="UTF-8"?>    
<SMS> 
<sender>Blabla</sender>    
<text>Hello</text>   
</message>    
<numbers> 
<number messageID="msg11">' . $phone . '</number> 
</numbers>   
</SMS>;

$Curl = curl_init();    
$CurlOptions = array(   
CURLOPT_URL=>'http://atompark.com/members/sms/xml.php',  
CURLOPT_FOLLOWLOCATION=>false,   
CURLOPT_POST=>true,  
CURLOPT_HEADER=>false,   
CURLOPT_RETURNTRANSFER=>true,    
CURLOPT_CONNECTTIMEOUT=>15,  
CURLOPT_TIMEOUT=>100,    
CURLOPT_POSTFIELDS=>array('XML'=>$src),   
);