php使用webservice调用C#服务端/调用PHP服务端
由于公司业务需要,用自产平台对接某大厂MES系统,大厂提出使用webservice来互通,一脸懵逼啊,一直没有使用过php的webservice的我,瞬间打开手册开始阅读,最终爬过无数坑之后,总结出如下几点
1.php充当服务端时 如果使用的是框架(TP,Laravel等) $server->setClass("app\index\controller\Oee"); 一定要是命名空间形式 否则找不到类 原生php的话直接写类名即可
2.服务端的'uri' 一定要和 客户端的 ‘uri’对应上
不多啰嗦了,进入正题,webservice有两种模式 一种是有 wsdl的 一种是无wsdl的
PHP充当客户端代码
$client = new \SoapClient('你的soap地址?wsdl');
$client->soap_defencoding = 'utf-8';
$client->xml_encoding = 'utf-8';
$xmlString = "<WorkRequest><Code>essa0001</Code><MaterialCode>2013</MaterialCode><MaterialName>1000</MaterialName><FinalMaterialQuantity>1000</FinalMaterialQuantity><ActualQuantity>800</ActualQuantity><PlanStartTime>2019-07-08</PlanStartTime><ActualStartTime>2019-07-09</ActualStartTime></WorkRequest>"; //参数
$param_ary = array( array("xmlString"=>$xmlString) ); //php请求C#后台 参数必须为二维数组
$client = $client->__soapCall('ProductionInfoPost',$param_ary); //php请求C#服务端 请求必须为__soapCall('方法名',参数)
//将XML转为数组 可省略
$array = (array)(simplexml_load_string($client->ProductionInfoPostResult));
foreach ($array as $key=>$item){
$array[$key] = $this->struct_to_array((array)$item);
}
dump($array);die;
C#服务端代码 自行百度 随便一个都可以用的哈
php充当客户端
ni_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存
$soap = new \SoapClient('http://你的soap网址/Service.php?wsdl');
$xml = "<PACKAGE><C3>**电信</C3><C4></C4><LINKMAN>张三</LINKMAN><LINKNUM>13412341234</LINKNUM><LINKADDRESS>广东深圳</LINKADDRESS><REMARK>iPhone6</REMARK><CHANNEL></CHANNEL><GRIDCODE>1111111111111111111111111111111</GRIDCODE><AGENTCODE>2111</AGENTCODE></PACKAGE>";
$res = $soap->Add($xml);
php充当服务端代码
Service.php
<?php
class Service { public function HelloWorld() {
return "Hello";
} public function Add($data) {
$data=array('data1'=>array('name'=>'张三','sex'=>'男','age'=>18),'data2'=>array('name'=>'李梅','sex'=>'女','age'=>20),'data3'=>array("name"=>'亨利','sex'=>'男','age'=>22)); return xml($data);
}
如果需要生成wsdl 附属下面生成wsdl代码
<?php
include("Service.php");
include("SoapDiscovery.php");
$disco = new SoapDiscovery('Service', 'soap'); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即Service类,第二个参数是服务的名字(这个可以随便写)。
$disco->getWSDL();
SoapDiscovery.php
<?php /**
* Copyright (c) 2005, Braulio Jos?Solano Rojas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* @version $Id$
* @copyright 2005
*/ /**
* SoapDiscovery Class that provides Web Service Definition Language (WSDL).
*
* @package SoapDiscovery
* @author Braulio Jos?Solano Rojas
* @copyright Copyright (c) 2005 Braulio Jos?Solano Rojas
* @version $Id$
* @access public
* */
class SoapDiscovery { private $class_name = '';
private $service_name = ''; /**
* SoapDiscovery::__construct() SoapDiscovery class Constructor.
*
* @param string $class_name
* @param string $service_name
* */
public function __construct($class_name = '', $service_name = '') {
$this->class_name = $class_name;
$this->service_name = $service_name;
} /**
* SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
*
* @return string
* */
public function getWSDL() {
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "<?xml version=\"1.0\" ?>\n";
$headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
$headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n"; if (empty($this->class_name)) {
throw new Exception('No class name.');
} $class = new ReflectionClass($this->class_name); if (!$class->isInstantiable()) {
throw new Exception('Class is not instantiable.');
} $methods = $class->getMethods(); $portTypeWSDL = '<portType name="' . $this->service_name . 'Port">';
$bindingWSDL = '<binding name="' . $this->service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
$serviceWSDL = '<service name="' . $this->service_name . "\">\n<documentation />\n<port name=\"" . $this->service_name . 'Port" binding="tns:' . $this->service_name . "Binding\"><soap:address location=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "\" />\n</port>\n</service>\n";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n";
$bindingWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<soap:operation soapAction="urn:' . $this->service_name . '#' . $this->class_name . '#' . $method->getName() . "\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
$messageWSDL.= '<message name="' . $method->getName() . "Request\">\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL.= '<part name="' . $parameter->getName() . "\" type=\"xsd:string\" />\n";
}
$messageWSDL.= "</message>\n";
$messageWSDL.= '<message name="' . $method->getName() . "Response\">\n";
$messageWSDL.= '<part name="' . $method->getName() . "\" type=\"xsd:string\" />\n";
$messageWSDL.= "</message>\n";
}
}
$portTypeWSDL.= "</portType>\n";
$bindingWSDL.= "</binding>\n";
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
//生成wsdl文件,将上面的return注释
$fso = fopen($this->class_name . ".wsdl", "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
} /**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
* */
public function getDiscovery() {
return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "?wsdl\" />\n</disco:discovery>";
} } ?>
大致就是这样 没有很难 只是很烦 坑太多 同平台肯比较少 如果跨平台 坑太多了 希望可以帮助小伙伴们少入坑吧
如果有小伙伴不明白 可以随时给我留言 帮你解决 共同学习
php使用webservice调用C#服务端/调用PHP服务端的更多相关文章
- C#调用WebService服务(动态调用)
原文:C#调用WebService服务(动态调用) 1 创建WebService using System; using System.Web.Services; namespace WebServi ...
- WebService-01-使用jdk发布第一个WebService服务并调用
Webservice是SOAP+XML,SOAP是基于Http的,Http底层是Socket,先回顾一下Socket: Server: public class Server { public sta ...
- WCF服务端调用client.
wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...
- 服务端调用接口API利器之HttpClient
前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...
- 一个php创建webservice,并通过c#调用的真实实例
最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜索和尝试,终于成功在C#下调用,JAVA的调用还没开始,为防止忘记,在这里记录下来全过程. 本文参考了许多文章,文中也采用了 ...
- C# WebService创建、发布、调用的简单例子
Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web ...
- C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式
C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...
- Python的Web编程[2] -> WebService技术[0] -> 利用 Python 调用 WebService 接口
WebService技术 / WebService Technology 1 关于webservice / Constants WebService是一种跨编程语言和跨操作系统平台的远程调用技术. W ...
- 一个php创建webservice,并通过c#调用的真实实例(转)
https://www.cnblogs.com/sequh/archive/2015/09/18/4819832.html 最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜 ...
- Https Webservice接口的免证书调用
目录 前言 思路 方案 Axis调用 HttpClient调用 参考链接 前言 在调用https协议的Webservice接口时,如果没有做证书验证,一般会报javax.net.ssl.SSLHand ...
随机推荐
- 怎样正确的理解和解决 ORA-01843:not a valid month
今天码代码的时候遇到了这个问题,由于oracle用的比較少,所在查询了一下. 顿时傻眼,有非常多的贴子说是由于nls_date_language的问题,还要改会话级的NLS_DATE_LANGUAGE ...
- wordpress如何判断手机、平板还是PC并显示对应的内容-Mobile Detect
wordpress如何判断是手机.平板还是PC访问,并针对性的显示特定的内容?Mobile Detect 这个轻量级PHP 类库能够很好的实现这个功能.而且Mobile Detect也有wordpre ...
- 安装Eclipse完PyDev插件中没有出现
假设你是在Window7在环境搭建.请确保您使用以管理员身份运行Eclipse. PyDev插件安装后没有显示是由于PyDev的执行须要Java7,能够通过升级JDK的版本号来完毕,而且配置环境变量( ...
- 通通玩blend美工(1)——荧光Button
原文:通通玩blend美工(1)--荧光Button 最近老大出差去了,光做项目也有点烦,写点教程消遣消遣(注:此乃初级教程,所以第一个消遣是本人消遣,第二个是指供各位看官消遣...) 看着各位大虾出 ...
- MVC 创建强类型视图
•在ViewModel中创建一个类型 •在Action中为ViewData.Model赋值 •在View中使用"@model类型"设置 14 手动创建强类型视图 •在ViewMod ...
- WPF自定义窗口最大化显示任务栏
原文:WPF自定义窗口最大化显示任务栏 当我们要自定义WPF窗口样式时,通常是采用设计窗口的属性 WindowStyle="None" ,然后为窗口自定义放大,缩小,关闭按钮的样式 ...
- win10 uwp 萤火虫效果
原文:win10 uwp 萤火虫效果 本文在Nukepayload2指导下,使用他的思想用C#写出来. 本文告诉大家,如何使用 win2d 做出萤火虫效果. 安装 win2d 安装win2d的方法请使 ...
- SQLServer 进程无法向表进行大容量复制(错误号: 22018 20253)
原文:SQLServer 进程无法向表进行大容量复制 我的环境:SQL SERVER 2008 R2:发布者 ->SQL SERVER 2017 订阅者 进程无法向表“"dbo&quo ...
- 解决手机提示TF卡受损需要格式化问题
昨晚因为上QQ FOR PAD后.关机.结果又杯具了.上次无意看到一个SD卡修复命令,收藏起来了.一试,还真管用.现把它写出来.分享给大家.以后出现SD卡受损,千万不要再格式化内存卡了.修复过程:1. ...
- Android多线程(三)
上次讲了关于Android多线程中通信中Thread.Handler.Looper等的基础概念和基本用法,用现实世界两个人写信交流的过程来理解是再好不过了.但是不得不说这一套完整的细节的确很繁琐,好在 ...