2021年上半年,内蒙古自治区人社厅发了《关于进一步加强“四位一体”就业服务云平台推广应用工作的通知》,要求各盟市人才网站与自治区四位一体就业服务云平台实现以接口方式对接。通辽市率先完成通辽市人才网的对接工作,这里把其中涉及的PHP和SOAP相关的技术难点和解决方案分享给大家,共同学习,共同进步。
文章目录
前言
通辽市人才网是通辽市人社局主办的官方招聘求职网站,是通辽地区最大的人才招聘门户网站,我作为本次与自治区四位一体就业服务云平台进行api对接的技术负责人,通过本次对接,也学习了不少的内容。上次与国家就业在线对接,难点主要是PHP版本的AES加密算法和HTTP CALLER以及中文乱码的处理,而本次则是PHP版本的SOAP和WebService,每次都是新的学习内容,让我在体制内也过了一把当程序员的瘾-。
提示:以下是本篇文章正文内容,下面案例可供参考
一、PHP通过SoapClient调用服务端接口
通辽市人才网向自治区四位一体就业服务云平台同步企业信息、职位信息和招聘会信息,均属于这种模式,在本地创建一个SoapClient,然后调用服务端提供的同步接口,代码如下(示例):
$soap = new SoapClient("http://xxxxxx/xxxx/xxxx/WebserviceSWYT?wsdl");
$result = $soap->setDb21($reqString);
二、PHP利用wsdl创建标准webservice
自治区四位一体就业服务云平台向通辽市人才网同步简历信息,属于这种模式,在通辽市人才网本地利用wsdl创建一个WebService,然后提供wsdl地址和对外接口给四位一体就业服务云平台调用,代码如下(示例):
1.创建文件SoapDiscovery.class.php
代码如下(示例):
<pre name="code" class="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>";
}
}
?>
2.提供服务的类或者函数
创建文件swyt_rcv_down_msg.class.php,里面有一个接收信息的接口dd03ByPerson
<?php
header("content-type:text/html;charset=utf-8");
class swyt_rcv_msg
{
public function dd03ByPerson ($strDd03)
{
$json_data['code'] = "1"; //
$json_data['message'] = "成功"; //
$req['data'] = $json_data;
$reqString = json_encode($req,JSON_UNESCAPED_UNICODE);
return $reqString;
}
}
3.生成WSDL
创建文件swyt_server.php,运行即可生成一个swyt_rcv_msg.wsdl文件:
<?php
header("content-type:text/html;charset=utf-8");
include("swyt_rcv_down_msg.class.php");
include("SoapDiscovery.class.php");
$disco = new SoapDiscovery('swyt_rcv_msg','swyt_rcv_msg');
$disco->getWSDL();
?>
4.创建webservice服务端程序
将swyt_server.php文件的内容清空,复制以下代码进去::
<?php
header("content-type:text/html;charset=utf-8");
include("swyt_rcv_down_msg.class.php");
$objSoapServer = new SoapServer("swyt_rcv_msg.wsdl");//swyt_rcv_msg.wsdl是刚创建的wsdl文件
$objSoapServer->setClass("swyt_rcv_msg");//注册swyt_rcv_msg类的所有方法
$objSoapServer->handle();//处理请求
?>
5.测试
代码如下(示例),传入地址填写swyt_rcv_msg.wsdl文件里面这个地址:
$client = new SoapClient("传入以上截图位置地址信息");
总结
最后我发一下通辽市人才网在内蒙古自治区四位一体就业服务云平台上面的截图,职位信息每天都会自动同步,极大地节约的企业的招聘成本,避免二次录入,提高了企业的招聘效率和扩展了企业的宣传面。