先上結果圖——
C# 代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
///ibmfashion 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
//若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class ibmfashion : System.Web.Services.WebService {
public ibmfashion () {
//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public int multiplication(int a, int b)
{
return a*b;
}
}
php調用c# webservice代碼:
<?php
// Pull in the NuSOAP code
ob_start();
require_once('lib/nusoap.php');
$url ="http://localhost:8787/wcf/ibmfashion.asmx?wsdl";
$client = new nusoap_client($url, 'wsdl','','','','');
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';
//參數轉換為數組傳遞
$ary = array('a' => 11, 'b' => 22);
$result = $client->call('multiplication',$ary);
echo "<pre>".print_r($result,true)."</pre>";
//錯誤及debug信息
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
總結php調用c# .NET webservice常用的幾種方法:
法1:
檢查System32目錄是否有php_soap.dll,如果沒有網上下載放到這個目錄下。
找到配置文件php.ini 文件, 打開以下擴展
extension = php_soap.dll
extension = php_curl.dll
extension = php_openssl.dll
PHP調用代碼如下:
方法1:
$url ="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8'
$result = $client->__soapCall("UserLogin",array("UserLogin"=>array(
'str' => '{"userName":"3","password":"222"}')));
if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}
方法2:
同樣用php_soap.dll,只是代碼略有不同:
$url ="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new SoapClient($url);
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';
$result = $client->UserLogin(array('str' => '{"userName":"3","password":"222"}'));
if (is_soap_fault($result)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo print_r("return:".$result->UserLoginResult,true);
}
方法3:
使用NuSoap是PHP環境下的WebService編程工具,用於創建或調用WebService。它是一個開源軟件,是完全采用PHP語言編寫的、通過HTTP收發SOAP消息的一系列PHP類,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )開發。NuSOAP的一個優勢是不需要擴展庫的支持,這種特性使得NuSoap可以用於所有的PHP環境,不受服務器安全設置的影響。
在處理過程中一定要注意WebService提供的參數是否匹配及正確。