php
  • xml
  • variables
  • 2014-04-02 59 views -1 likes 
    -1

    請幫助我,比如下一個我:PHP變量轉換成XML

    <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), 
    ); 
    ?> 
    

    我怎樣才能把變量$的手機變成標籤XML? 非常感謝

    回答

    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), 
    ); 
    
    3

    您的代碼是錯誤的。

    • 您正在關閉「</message>」而未打開。
    • 您必須使用'before;'關閉$ 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), 
        ); 
    

    阿波羅

    +0

    他爲什麼要這麼做的'$捲曲= curl_init();'第二部分? –

    +1

    我的不好,我編輯它。 – Apolo

    相關問題