码迷,mamicode.com
首页 > 其他好文 > 详细

用post来进行Soap请求

时间:2017-07-12 00:55:08      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:post   nbsp   创建   basic   app   env   his   ref   数组   

最近调了一个Soap请求C# webservice的项目。网上坑不少。

使用原生的SoapClient库请求也是失败。只好用post来进行模拟了。代码贴出来,给大家参考一下。

 

<?php

namespace App\Services\Proxy;

use Log;

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‘);
    }
    
    /**
     * vipCreate
     */
    public function vipCreate($mobile, $openid, $username, $sex)
    {
        $this->values = [
            ‘vip‘ => [
                ‘surname‘ => $username,
                ‘mobile‘ => $mobile,
                ‘weixin‘ => $openid,
                ‘sex‘ => ($sex == 1) ? ‘M‘ : ‘F‘,
                ‘xf_vipcodeprefix‘ => ‘220‘,
            ]
        ];

        $response = $this->response(‘VipCreate‘, ‘vip.mobile‘);
        if (!empty($response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘DATA‘][‘xf_vipcode‘])) {
            Log::info(‘CRM创建用户:‘ . $response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘DATA‘][‘xf_vipcode‘]);
            return $response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘DATA‘][‘xf_vipcode‘];
        } elseif (isset($response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘Header‘][‘ERRCODE‘])) {
            Log::error(‘CRM创建用户失败,错误代码:‘ 
                        . $response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘Header‘][‘ERRCODE‘]
                        . ‘,错误信息:‘ 
                        . $response[‘Body‘][‘VipCreateResponse‘][‘VipCreateResult‘][‘Header‘][‘ERRMSG‘]);
        }

        return null;
    }

    /**
     * VipModify
     */
    public function vipModify($user)
    {
        $this->values = [
            ‘vip‘ => [
                ‘xf_vipcode‘ => $user[‘crm_vipcode‘],
                ‘surname‘ => $user[‘username‘],
                ‘weixin‘ => $user[‘open_id‘],
                ‘birthday‘ => $user[‘birthday‘],
                ‘idcardno‘ => $user[‘ID_number‘],
                ‘address‘ => $user[‘address‘],
                ‘jointdate‘ => date(‘Y-m-d H:i:s‘, $user[‘created_at‘]),
                ‘sex‘ => ($user[‘sex‘] == 1) ? ‘M‘ : ‘F‘,
                ‘xf_vipcodeprefix‘ => ‘220‘,
            ]
        ];

        $response = $this->response(‘VipModify‘, ‘vip.xf_vipcode‘);

        if (isset($response[‘Body‘][‘VipModifyResponse‘][‘VipModifyResult‘][‘Header‘][‘ERRCODE‘])) {
            if (empty($response[‘Body‘][‘VipModifyResponse‘][‘VipModifyResult‘][‘Header‘][‘ERRCODE‘])) {
                Log::info(‘CRM修改用户:‘ . $user[‘username‘] . ‘成功‘ . json_encode($this->values));
                return true;
            } else {
                Log::error(‘CRM创建用户失败,错误代码:‘
                            . $response[‘Body‘][‘VipModifyResponse‘][‘VipModifyResult‘][‘Header‘][‘ERRCODE‘]
                            . ‘,错误信息:‘ 
                            . $response[‘Body‘][‘VipModifyResponse‘][‘VipModifyResult‘][‘Header‘][‘ERRMSG‘]);
            }
        } 

        return false;
    }

    /**
     * getBasicInfo
     */
    public function getBasicInfo()
    {
        dd($this->response(‘GetBasicInfo‘));
        return $this->response(‘GetBasicInfo‘);
    }

    /**
     * 以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;
    }
}

 

用post来进行Soap请求

标签:post   nbsp   创建   basic   app   env   his   ref   数组   

原文地址:http://www.cnblogs.com/xuhuaiqu/p/7152808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!