php 调用c# .NET 写的webservice(亲测通过)

先上结果图——

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提供的参数是否匹配及正确。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值