说明:php调用.net WebService,解决返回消息格式问题,比如数据库查询结果。本方案将.net DataTable序列化为JSON返回给php,php使用json_decode()解码JSON数据为数组。
[WebMethod] public string dsMysql() { DataSet dataset = new DataSet(); MySqlConnection conn = new MySqlConnection("Database=test;User Id=root;Password=ikmbikmb;Data Source=192.168.1.121;Port=3306;Pooling=true;Max Pool Size=10;Min Pool Size=1"); MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = new MySqlCommand("SELECT * FROM t1", conn); adapter.Fill(dataset); JSON_Class Object_JSON_Class = new JSON_Class(); string tb = Object_JSON_Class.CreateJsonParameters(dataset.Tables[0]); return tb; }
<?php $link = mysql_pconnect("192.168.1.121", "root", "ikmbikmb") or die("Could not connect : " . mysql_error()); mysql_select_db("test") or die("Could not select database"); $query = "SELECT * FROM t1"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); print "<table>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; mysql_free_result($result); mysql_close($link); ?>
<?php $objSoapClient = new SoapClient("http://192.168.1.119/Service1.asmx?WSDL"); $out=$objSoapClient->dsMysql(); $json=$out->dsMysqlResult; $t=json_decode($json, true); $t=$t['Head']; print "<table>\n"; foreach ($t as $line) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } ?>