php修改xml
① php解析xml
.asmx的全程是ASMX Active Server Methods,.asmx 是WEB服務文件,asmx.cs里有相關代碼
屬於B/S形式,用SOAP方式HTTP訪問,用XML返回。
以下是 php語言 利用 soap調用.Net的WebService asmx文件
<?php
//php.ini中打開下面3個dll
//extension = php_soap.dll
//extension = php_curl.dll
//extension = php_openssl.dll
header("content-type:text/html;charset=utf-8");
$client = new SoapClient(" http://192.168.1.178:808/ChkWelePsw.asmx?WSDL");
//本行測試不可行 $client = new SoapClient(" http://192.168.1.178:808/chkwelepsw.asmx?WSDL/ChkWele?username=test3&psw=123");
//參數這樣傳遞 先包裝一下
$param = array('username'=>'test3','psw'=>'123');
//調用必須用__soapCall
$p = $client->__soapCall('ChkWele',array('parameters' => $param));
print_r($p->ChkWeleResult); //這里先輸出一下變數$p,看看是什麼類型。
?>
.NET部分 webservice要注意的地方
/*
* <system.web>在這個節點中加入如下內容
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>
*/
[WebMethod(Description = "This......", EnableSession = false)]
public string ChkWele(string username, string psw)
{
string ret = "";
return ret;
}
② php把xml轉換為字元串
樓主我教你吧,首先xml文件里的內容為
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<content
<name>lishi</name>
<age>17</age>
</content>
讀取xml文件內容
$str=file_get_contents($xml);$xml為xml文件路徑地址
將讀取的字元串內容轉化為xml對象
$obj=simplexml_load_string($str)
操作對象里的數據
$obj->name="lishi111";
$obj->age=77;
拼接新的字元串
$strNew="<?xml version='1.0' encoding='ISO-8859-1'?>";
$str.="<content><name>".$obj->name."</name";
$str.="<age>".$obj->age."</age></content>";
將新的字元串寫入xml文件
file_put_content($xml,$strNew);
最後一步拿分來吧,哈哈哈哈。有問題繼續。。。
③ PHP讀取xml文件
以前做過聯通或電信的service訂購介面,收到的內容就是XML的,需要用到PHP的XML處理功能,你的代碼可以這樣:
<xmp>
<?php
$string=file_get_contents("http://www.orderjiaju.com/zixun/data/rss/3.xml");
if($string!=""):
$xml = new DomDocument('1.0');
$xml->loadXML($string);
//班台
$BanTai=array(
'Title'=>$xml->getElementsByTagName('channel')->item(0)->childNodes->item(1)->nodeValue,
'Link' =>$xml->getElementsByTagName('channel')->item(0)->childNodes->item(3)->nodeValue
);
for($i=0;$i<3;$i++)
{
$Title[]=array(
'Title'=>$xml->getElementsByTagName('item')->item($i)->childNodes->item(1)->nodeValue,
'Link' =>$xml->getElementsByTagName('item')->item($i)->childNodes->item(3)->nodeValue
);
}
print_r($BanTai);
print_r($Title);
endif;
?>
</xmp>
回答補充:
現在你直接,然後執行沒效果嗎?
我這里是可以的呢,測試網址是:service.020i.net/test_xml.php
④ php將XML轉換成字元串!
$str = $xml->asXML();
$str就是你要的字元串