首页 > 编程语言 >php 调用c# .NET 写的webservice(亲测通过)

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

时间:2022-10-20 10:01:21浏览次数:47  
标签:webservice c# System echo client result NET php soap


先上结果图——

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

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环境,不受服务器安全设置的影响。


require_once("nusoap/Lib/nusoap.php");
$url ="http://www.ws.cn/1/Healthgrow/Service.asmx?wsdl";
$client = new nusoap_client($url, 'wsdl','','','','');
$client->soap_defencoding='utf-8';
$client->decode_utf8=false;
$client->xml_encoding='utf-8';

//参数转换为数组传递
$ary = array(array('str' => '{"userName":"3","password":"222"}'));
$result = $client->call('UserLogin',$ary);
echo "<pre>".print_r($result["UserLoginResult"],true)."</pre>";

在处理过程中一定要注意WebService提供的参数是否匹配及正确。

标签:webservice,c#,System,echo,client,result,NET,php,soap
From: https://blog.51cto.com/u_5112239/5777705

相关文章

  • git 阻止在某个分支上面提交commit
    比如在开发中不希望master分支被commit做提交,那么我们可以这样做找到.git/hook/文件夹然后在里面复制一个pre-commit出来cd.git/hooks/cp然后编辑它的第二行类似于这样......
  • Vue 中 created 和 mounted 的区别
    大多数人在谈论生命周期钩子时会感到困惑的一件事是 ​​created​​​ 和 ​​mounted​​之间的区别。有着相似的名称,觉得应该做同样的事情,但还是有一些细微的差别。首......
  • leetcode 最长回文子串
    constcountSubstrings=(s)=>{conststrLen=s.length;letnumOfPalindromicStr=0;//初始化一个二维数组letdp=Array.from(Array(strLen),()=>A......
  • 实验2 C语言控制语句应用编程
    task1.c源代码:1#include<stdio.h>2#include<time.h>3#include<stdlib.h>4#defineN55intmain(){6intnumber;7inti;89s......
  • 使用dotnet命令发布跨平台项目对应平台的程序包
    通过命令创建跨平台项目对应平台的程序包:分别生成win-x64、linux-x64、osx-x64平台的程序包win-x64:在项目文件中新增配置:<RuntimeIdentifiers>win-x64</RuntimeIdenti......
  • JDBC-快速入门和DriverManager注册驱动
    JDBC-快速入门步骤:1.导入驱动jar包(依赖也可以)<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.......
  • Javascript--变量内函数句柄
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><p>创建和使用对象方法。</p><p>对象方法作为一个函数定义存储......
  • mysql函数 字符长度限制_MySQL中使用group_concat()函数数据字符过长报错的问题解决方
    selectGROUP_CONCAT(uid)asuids,spread_uidfromeb_user_spreadwhereuid<>spread_uidGROUPBYspread_uid使用GROUP_CONCAT函数将字符串连接起来,数据量大的时候,会......
  • Vue前端的 Excel 导入和导出功能
    Excel解析为JSON基本内容组件效果和结构组件内容是很简单的结构和视图,直接查看如下的页面效果和代码即可:  <template><inputtype="file"ref="exc......
  • CF 1012C. Hills 题解
    题目传送门:Link。算法:DP。设计状态第一眼看着道题就感觉像是DP,再观察数据范围大概是\(O(n^2)\)的时间复杂度。因为要求多个\(k\)的答案,那么状态第一维显然是令多......