PHP用post来进行Soap请求
最近调了一个Soap请求C# webservice的项目。网上坑不少。
使用原生的SoapClient库请求也是失败。只好用post来进行模拟了。代码贴出来,给大家参考一下。
<?php namespace App\Services\Proxy; use Log;
use Cache; class Crm
{
private $host; private $namespace; private $app_secret; private $username; private $values; public function __construct()
{
$this->host = config('crm.host');
$this->namespace = config('crm.namespace');
$this->app_secret = config('crm.app_secret');
$this->username = config('crm.username');
$this->values = [];
} /**
* 通过mobile和open_id获取用户信息
*/
public function getVipInfoByMobileOpenID($mobile, $openid)
{
$this->values = [
'mobile' => $mobile,
'openid' => $openid,
]; return $this->response('GetVipInfoByMobileOpenID');
} /**
* 以post方式提交xml到对应的接口url
*/
private function postXml($action, $sign_index = null)
{
$body = $this->toCRMXml($action, $sign_index); // Get cURL resource
$ch = curl_init(); // Set url
curl_setopt($ch, CURLOPT_URL, $this->host); // Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // Set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml"]); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // Send the request & save response to $resp
$resp = curl_exec($ch); if ($resp === false) {
$error = curl_error($ch);
curl_close($ch); Log::info('CRM请求错误:' . json_encode($error));
return false;
} curl_close($ch);
return $resp;
} /**
* 输出CRM soap xml字符
*/
private function toCRMXml($action, $sign_index = null)
{
$soap_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"; $soap_xml .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tech=\"{$this->namespace}\">\r\n";
$soap_xml .= "<soap:Body>\r\n";
$soap_xml .= "<$action xmlns=\"{$this->namespace}\">\r\n";
$soap_xml .= "<request>\r\n"; // header
$soap_xml .= $this->setHeader($soap_xml, $sign_index); // data
$soap_xml .= "<Data>\r\n";
$soap_xml .= $this->arrayToXml($this->values);
$soap_xml .= "</Data>\r\n";
$soap_xml .= "</request>\r\n"; $soap_xml .= "</$action>\r\n"; $soap_xml .= "</soap:Body>\r\n";
$soap_xml .= "</soap:Envelope>"; return $soap_xml;
} /**
* 生成Header
*/
private function setHeader($xml, $sign_index= null)
{
list($date, $time) = explode(' ', date('Ymd His'));
$sign = $this->setSign($date, $time, $sign_index); $xml = "";
$xml .= "<Header>\r\n";
$xml .= "<SIGN>$sign</SIGN>\r\n";
$xml .= "<REQDATE>$date</REQDATE>\r\n";
$xml .= "<REQTIME>$time</REQTIME>\r\n";
$xml .= "<USER>{$this->username}</USER>\r\n";
$xml .= "</Header>\r\n"; return $xml;
} /**
* 生成sign
*/
private function setSign($date, $time, $sign_index= null)
{
if ($sign_index) {
if (strpos($sign_index, '.')) {
list($stuct_index, $index) = explode('.', $sign_index);
$seeder = $date . $time . $this->values[$stuct_index][$index] . $this->app_secret;
} else {
$seeder = $date . $time . $this->values[$sign_index] . $this->app_secret;
}
} else {
$seeder = $date . $time . $this->app_secret;
} return md5($seeder);
} /**
* 数组转换成xml
*/
private function arrayToXml($params)
{
$xml = "";
foreach ($params as $name => $value) {
if (is_array($value)) {
$xml .= "<$name>" . $this->arrayToXml($value) . "</$name>\r\n";
} else {
$xml .= "<$name>$value</$name>\r\n";
}
} return $xml;
} /**
* 将xml结果转化为对象
*/
public function response($action, $sign_index = null)
{
$result = str_ireplace('soap:', '', $this->postXml($action, $sign_index));
return $this->objectToArray(simplexml_load_string($result, 'SimpleXMLIterator', LIBXML_NOCDATA));
} /**
* 将对象转化为数组
*/
public function objectToArray($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj; $arr = [];
foreach ($_arr as $key => $val) {
$val = (is_array($val) || is_object($val)) ? $this->objectToArray($val) : $val;
$arr[$key] = $val;
} return $arr;
}
}
PHP用post来进行Soap请求的更多相关文章
- Axis2(10):使用soapmonitor模块监视soap请求与响应消息
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...
- Java发布一个简单 webservice应用 并发送SOAP请求
一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...
- Java发布webservice应用并发送SOAP请求调用
webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...
- [Postman]发出SOAP请求(18)
使用Postman发出SOAP请求: 将SOAP端点作为URL.如果您使用的是WSDL,那么请将WSDL的路径作为URL. 将请求方法设置为POST. 打开原始编辑器,并将正文类型设置为“text / ...
- java 查看SOAP请求报文
log.info("ESB 请求URL = " + cachedEndpoint.toString());//打印SOAP请求报文 add by LinJC on 20170120 ...
- C# httpRequest Soap请求
一般添加web服务引用是.NET用代理类模式 创建SOAP请求代理类,代理类是.NET开发工具VS自动给你生成. 下面用一般HTTP的模式有时候可能更合适,原理是构造SOAP请求的XML后POST过去 ...
- iOS webservice SOAP 请求
1. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, 无论它们所使用的语 ...
- [转]C#通过Http发送Soap请求
/// <summary> /// 发送SOAP请求,并返回响应xml /// </summary> /// <param na ...
- Web Service之Soap请求响应内容中文编码解密
java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式.比如: 中文:退保成功 Soap中文编码:退保成功 我仔细分析后发现,退编码实际上 ...
随机推荐
- nginx 1.14.2 配置文件优化精选
user nobody; worker_processes ; worker_rlimit_nofile ; events { use epoll; worker_connections ; } ht ...
- 登录获取session
- HTTP请求与接收get/post方式
//get方式 public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebReq ...
- 20165306 Exp2 后门原理与实践
20165306 Exp2 后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF mete ...
- Go语言库之strconv包(转载自--http://blog.csdn.net/alvine008/article/details/51283189)
golang strconv.ParseInt 是将字符串转换为数字的函数 func ParseInt(s string, base int, bitSize int) (i int64, err e ...
- java在window下用cmd (javac、jar)命令行模拟Intellij IDEA软件生成jar包
@@首先最重要的cmd命令: javac ,jar :使用如下(注意[.]不要输错): 1. javac编译: D:\MyWorkSet\idea_hadoop>javac -d .\out\p ...
- JVM-GC学习
http://blog.csdn.net/column/details/14851.html 地址记录
- 在线批量将gps经纬度坐标转换为百度经纬度坐标
1.首先打开百度api示例页面: 在浏览器地址栏中输入:http://developer.baidu.com/map/jsdemo.htm#a5_3 2.修改代码 如下图,将需要批量转换的坐标,按规则 ...
- linux 内存-文档学习
ptmalloc http://www.malloc.de/en/ tcmalloc https://github.com/gperftools/gperftools jcmalloc http:// ...
- 雷林鹏分享:CodeIgniter 数据库操作
在平时项目开发过程中,除了处理那些繁琐的逻辑过程外,还有一个重要的任务就是对数据库的操作处理.这里总结下自己平时使用CI过程中使用的如何对数据库进行操作处理. 在CI框架中,我们一般会把对数据库的操作 ...