8 changed files with 316 additions and 37 deletions
@ -0,0 +1,19 @@ |
|||
<?php |
|||
declare (strict_types=1); |
|||
|
|||
namespace app\controller\api; |
|||
|
|||
use app\service\webService\FeeService; |
|||
use NuSoap\Client\Client; |
|||
|
|||
class Test extends Base |
|||
{ |
|||
|
|||
public function index() |
|||
{ |
|||
$FeeService = new FeeService('41107394'); |
|||
dump($FeeService->getUserTotal()); |
|||
die; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,272 @@ |
|||
<?php |
|||
|
|||
namespace app\service\webService; |
|||
|
|||
use fast\FuncException; |
|||
|
|||
class FeeService |
|||
{ |
|||
|
|||
protected $wsdl; |
|||
protected $port; |
|||
protected $pucode; |
|||
|
|||
const ONE_KEY = 'SOAP-ENV:Body'; |
|||
|
|||
public function __construct($pucode) |
|||
{ |
|||
$this->wsdl = env('wsdl.soap_url'); |
|||
$this->port = env('wsdl.soap_port'); |
|||
$this->pucode = $pucode; |
|||
} |
|||
|
|||
/** |
|||
* 查询用户数量 |
|||
* @return mixed |
|||
* @throws FuncException |
|||
*/ |
|||
public function getUserTotal() |
|||
{ |
|||
|
|||
$arr = ['pucode' => $this->pucode]; |
|||
|
|||
$method = 'GetUserTotal'; |
|||
|
|||
$res = $this->curl($method, $this->xml_str($arr)); |
|||
|
|||
$key = 'NS1:' . $method . 'Response'; |
|||
$this->dataValidate($res, $key); |
|||
|
|||
return $res[$key]; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 查询用户基本信息 |
|||
* @return mixed |
|||
* @throws FuncException |
|||
*/ |
|||
public function getUsers(): array |
|||
{ |
|||
$arr = ['pucode' => $this->pucode]; |
|||
|
|||
$method = 'GetUsers'; |
|||
|
|||
$res = $this->curl($method, $this->xml_str($arr)); |
|||
|
|||
$key = 'NS1:GetUsersResponse'; |
|||
$this->dataValidate($res, $key); |
|||
|
|||
$key2 = 'NS2:TUserInfo'; |
|||
$this->dataValidate($res[$key], $key2, 2); |
|||
|
|||
return $res[$key][$key2]; |
|||
} |
|||
|
|||
/** |
|||
* 查询用户表资料 |
|||
* @return mixed |
|||
* @throws FuncException |
|||
*/ |
|||
public function getWaterMeters() |
|||
{ |
|||
|
|||
$arr = ['userCode' => $this->pucode]; |
|||
|
|||
$method = 'GetWaterMeters'; |
|||
|
|||
$res = $this->curl($method, $this->xml_str($arr)); |
|||
|
|||
$key = 'NS1:GetWaterMetersResponse'; |
|||
$this->dataValidate($res, $key); |
|||
|
|||
$key2 = 'NS2:TWaterMeter'; |
|||
$this->dataValidate($res[$key], $key2, 2); |
|||
|
|||
return $res[$key][$key2]; |
|||
} |
|||
|
|||
/** |
|||
* 查询用户抄表信息 |
|||
* @param $param ['startCostYearMonth' => '2024-01','endCostYearMonth' => '2024-02'] |
|||
* @throws FuncException |
|||
*/ |
|||
public function getReadingDetail(array $param = []) |
|||
{ |
|||
|
|||
$arr = ['userCode' => $this->pucode]; |
|||
|
|||
if ($param) { |
|||
$arr = array_merge($arr, $param); |
|||
} |
|||
|
|||
$method = 'GetReadingDetail'; |
|||
|
|||
$res = $this->curl($method, $this->xml_str($arr)); |
|||
|
|||
$key = 'NS1:GetReadingDetailResponse'; |
|||
$this->dataValidate($res, $key); |
|||
|
|||
$key2 = 'NS2:TUserReading'; |
|||
$this->dataValidate($res[$key], $key2, 2); |
|||
|
|||
return $res[$key][$key2]; |
|||
} |
|||
|
|||
/** |
|||
* 查询用户费用信息 |
|||
* @param $param ['startCostYearMonth' => '2024-01','endCostYearMonth' => '2024-02','PaymentStatus' => '0'] |
|||
* PaymentStatus 0:所有 1:未缴 2:已缴 5:部分缴 |
|||
* @throws FuncException |
|||
*/ |
|||
public function getComputeDetail(array $param = []) |
|||
{ |
|||
|
|||
$arr = ['userCode' => $this->pucode]; |
|||
|
|||
if ($param) { |
|||
$arr = array_merge($arr, $param); |
|||
} |
|||
|
|||
$method = 'GetComputeDetail'; |
|||
|
|||
$res = $this->curl($method, $this->xml_str($arr)); |
|||
|
|||
$key = 'NS1:GetComputeDetailResponse'; |
|||
$this->dataValidate($res, $key); |
|||
|
|||
$key2 = 'NS2:TComputeDetail'; |
|||
$this->dataValidate($res[$key], $key2, 2); |
|||
|
|||
return $res[$key][$key2]; |
|||
} |
|||
|
|||
protected function xml_str($arr): string |
|||
{ |
|||
$xml_arr = []; |
|||
|
|||
foreach ($arr as $key => $value) { |
|||
$xml_arr[] = "<{$key}>" . $value . "</{$key}>"; |
|||
} |
|||
|
|||
return implode("\r\n", $xml_arr); |
|||
} |
|||
|
|||
/** |
|||
* POST 调用xml接口 |
|||
* @param $method |
|||
* @param $xml_filed |
|||
* @return array |
|||
* @throws FuncException |
|||
*/ |
|||
protected function curl($method, $xml_filed): array |
|||
{ |
|||
$xml = <<<XML |
|||
<?xml version="1.0"?> |
|||
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" |
|||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> |
|||
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http:schemas.xmlsoap.org/soap/encoding/"> |
|||
<NS1:{$method} xmlns:NS1="urn:FeeServiceIntf-IFeeService"> |
|||
{$xml_filed} |
|||
</NS1:{$method}> |
|||
</SOAP-ENV:Body> |
|||
</SOAP-ENV:Envelope> |
|||
XML; |
|||
|
|||
$wsdl = 'http://120.234.15.170:4000/FeeService.dll/soap/IFeeService'; |
|||
|
|||
// 初始化cURL会话 |
|||
$ch = curl_init(); |
|||
|
|||
// 设置cURL选项 |
|||
curl_setopt($ch, CURLOPT_URL, $wsdl); // 目标URL |
|||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出 |
|||
curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求 |
|||
|
|||
// 设置POST字段 |
|||
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); |
|||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); // 设置HTTP头 |
|||
|
|||
// 执行cURL会话 |
|||
$response = curl_exec($ch); |
|||
|
|||
// 检查是否有错误发生 |
|||
if (curl_errno($ch)) { |
|||
throw new FuncException('cURL error: ' . curl_error($ch)); |
|||
} |
|||
|
|||
// 关闭cURL会话 |
|||
curl_close($ch); |
|||
|
|||
$array = $this->getXmlDom($response); |
|||
|
|||
return $this->curlValidate($array); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $response |
|||
* @return array |
|||
*/ |
|||
protected function getXmlDom($response): array |
|||
{ |
|||
$dom = new \DOMDocument(); |
|||
$dom->loadXML($response); |
|||
|
|||
return $this->xmlToArray($dom->documentElement); |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $node |
|||
* @return array|mixed |
|||
*/ |
|||
protected function xmlToArray($node) |
|||
{ |
|||
$output = []; |
|||
|
|||
if ($node->hasChildNodes()) { |
|||
foreach ($node->childNodes as $child) { |
|||
if ($child->nodeType === XML_ELEMENT_NODE) { |
|||
$output[$child->nodeName] = $this->xmlToArray($child); |
|||
} elseif ($child->nodeType === XML_TEXT_NODE) { |
|||
$output = $child->nodeValue; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return $output; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $array |
|||
* @return mixed |
|||
* @throws FuncException |
|||
*/ |
|||
protected function curlValidate($array) |
|||
{ |
|||
|
|||
if (!isset($array[self::ONE_KEY])) { |
|||
throw new FuncException('数据获取失败 -0'); |
|||
} |
|||
|
|||
return $array[self::ONE_KEY]; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param $data |
|||
* @param $key |
|||
* @param int $level |
|||
* @throws FuncException |
|||
*/ |
|||
public function dataValidate($data, $key, int $level = 1) |
|||
{ |
|||
if (!isset($data[$key])) { |
|||
throw new FuncException("数据获取失败 -{$level}"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue