由于公司业务需要,用自产平台对接某大厂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服务端的更多相关文章

  1. C#调用WebService服务(动态调用)

    原文:C#调用WebService服务(动态调用) 1 创建WebService using System; using System.Web.Services; namespace WebServi ...

  2. WebService-01-使用jdk发布第一个WebService服务并调用

    Webservice是SOAP+XML,SOAP是基于Http的,Http底层是Socket,先回顾一下Socket: Server: public class Server { public sta ...

  3. WCF服务端调用client.

    wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...

  4. 服务端调用接口API利器之HttpClient

    前言 之前有介绍过HttpClient作为爬虫的简单使用,那么今天在简单的介绍一下它的另一个用途:在服务端调用接口API进行交互.之所以整理这个呢,是因为前几天在测试云之家待办消息接口的时候,有使用云 ...

  5. 一个php创建webservice,并通过c#调用的真实实例

    最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜索和尝试,终于成功在C#下调用,JAVA的调用还没开始,为防止忘记,在这里记录下来全过程. 本文参考了许多文章,文中也采用了 ...

  6. C# WebService创建、发布、调用的简单例子

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web ...

  7. 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 ...

  8. Python的Web编程[2] -> WebService技术[0] -> 利用 Python 调用 WebService 接口

    WebService技术 / WebService Technology 1 关于webservice / Constants WebService是一种跨编程语言和跨操作系统平台的远程调用技术. W ...

  9. 一个php创建webservice,并通过c#调用的真实实例(转)

    https://www.cnblogs.com/sequh/archive/2015/09/18/4819832.html 最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜 ...

  10. Https Webservice接口的免证书调用

    目录 前言 思路 方案 Axis调用 HttpClient调用 参考链接 前言 在调用https协议的Webservice接口时,如果没有做证书验证,一般会报javax.net.ssl.SSLHand ...

随机推荐

  1. crossplatform----文本编辑器工具Atom安装

    1.简介 Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器.具有简洁和直观的图形用户界面,并有很多有趣的特点:支持CSS,HTML,JavaScript等网页编程语言.它支持宏,自动 ...

  2. python 教程 第十四章、 地址薄作业

    第十四章. 地址薄作业 #A Byte of Python #!/usr/bin/env python import cPickle import os #define the contacts fi ...

  3. OpenGL(八) 显示列表

    OpenGL在即时模式(Immediate Mode)下绘图时,程序中每条语句产生的图形对象被直接送进绘图流水线,在显示终端立即绘制出来.当需要在程序中多次绘制同一个复杂的图像对象时,这种即时模式会消 ...

  4. [数字dp] hdu 3271 SNIBB

    意甲冠军:有两个查询: q=1.在[x,y]间隔,兑换b十进制,数字和m多少个月. q=2.在[x,y]间隔,兑换b十进制,数字是m第一k的数目是多少(十进制),没有输出由给定的主题. 思维: 和比特 ...

  5. Win10中解决Prolific PL2303出现错误代码10的问题

    PL2303 是Prolific 公司生产的一种高度集成的RS232-USB接口转换器,在Win10中默认安装的驱动程序会出现错误代码10的问题,如下图所示: 下载Win10上可以用的PL2303驱动 ...

  6. .Net Core中使用NodeJs加解密DES,MD5,AES,REA

    鉴于使用.net core我们的加解密也同时迁移到了跨平台上,我使用的是NodeJs加解密的.废话不多说了,还是来干活吧. 1.创建Node项目 2.添加package.json {  "n ...

  7. UWP 浏览本地图片及对图片的裁剪

    原文:UWP 浏览本地图片及对图片的裁剪 1.前言 准备给我的校园助手客户端添加一个修改头像的功能,但是查了好多资料都没有找到裁剪图片的简单的方法,最后才找到这个使用Launcher调用系统组件的简单 ...

  8. WPF判断两个PNG图片是否碰撞

    这个方法有几个前提 1.两个Image必须在一个Canvas中 2.两个Image的Canvas.Top和Canvas.Left必须赋值 上一篇讲了判断一个PNG图片某个点是否透明 这个基本类似的方法 ...

  9. ARTS 1.21 - 1.25

    每周一个 Algorithm,Review 一篇英文文章,总结一个工作中的技术 Tip,以及 Share 一个传递价值观的东西! Algorithm: 学习算法 题目:3Sum Closest 解题过 ...

  10. Windows 10 UWP 部署

      原文  http://youthlin.com/20151105.html 我们知道VS连接手机可以直接部署到手机里,但平板貌似无法这样干,平板与电脑连接没有丝毫反应……那么想看VS里写的uwp应 ...