php handles wsdl

Oct 20, 2016 pm 02:18 PM

0x00 Preface

I have been writing interfaces recently. Before this, interface data transmission was transmitted or obtained using json or xml format. But this time when jointly debugging with a third party, they provided the wsdl format. Changed to SB in an instant...

Google the test code, and the test calls the third-party interface to return status 200. I thought it would be over if nothing happened, but when I got closer, I discovered that no matter how I called their interface, the correct data was not returned. Later, after checking the log, they found that they had not received the passed parameters at all. They were confused for an afternoon and into the evening before they solved the problem. I thought it was quite interesting, so I wrote it down first.

0x01 What is wsdl? Based on what someone said, it is an xml format document used to describe the definition of Web Server, that is to say, it is a Web Server method and parameter description.

See: http://baike.baidu.com/link?url=R7x3FdekxbndR4SlzQLZE_2m1ebpt_SWt9IMjoHSErvLlbZ3-hwhR3ERrinXS1xZaDvkYFpxWnUchrk34_WkZq

When we request http://api.test.cn/xwebservice s/testServer?wsdl', what ends like this? When setting the URL of wsdl, a bunch of xml structured data will be given to you.

That's right, it's just a lump...

php handles wsdlNext, how to understand it and the method it says is the key, everything else is useless.

0x02 Understanding the description document

When I first looked at this xml document, I was confused, but it became much more obvious after using the PHP extension.

<?php
$client = new SoapClient(&#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;);

print "\n提供的方法\n";
print_r($client->__getFunctions());
print "相关的数据结构\n";
print_r($client->__getTypes());
print "\n\n";
Copy after login

php handles wsdlHere we use the SOAP extension. This extension is the operation processing WebServer service extension provided in the official PHP copy. In the end, we also use it to realize parameter transmission.

As can be understood from the above picture, this interface provides three methods, namely:

xxxxUserInfo

xxxxResumeNum

download**

The relevant data structure refers to the parameter name and parameter type in the method . For example, the xxxxUserInfo method requires three string type parameters. Correspond to in0, in1 and in2 respectively.

Note

The parameter key here must be in0, which is an arbitrary parameter name that does not require an array, user-defined or agreed upon by both parties. When I started writing the interface method, I performed the transmission based on the parameter descriptions given in the interface copy, such as: err_msg (indicating error information), err_code (indicating error coding), and date (final data transmitted). Then change it to an ordered array and fill in the corresponding parameters one by one. At this time, the key is 0 to 2. But after trying it, it still didn't work. Finally, with the mentality of giving it a try, I tried using int0 as the key name and the corresponding err_msg content as the value. OK~, perfect solution.

Code:

<?php

/**
 * @author 0x584A
 * 获取WSDL接口数据
 */
class getwsdlTest extends PHPUnit_Framework_TestCase
{
    public $apiurl = &#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;;
    private static $soapClientHandler;
    private $infoArr = [
        &#39;err_msg&#39; => &#39;false&#39;,
        &#39;err_code&#39; => &#39;0&#39;,
        &#39;date&#39; => &#39;此处是要传输的数据&#39;
    ];

    public function setUp()
    {
        $client = new SoapClient(&#39;http://api.test.cn/xwebservices/testServer?wsdl&#39;);
        print "提供的方法\n";
        print_r($client->__getFunctions());
        print "相关的数据结构\n";
        print_r($client->__getTypes());
        print "\n\n";
    }

    /**
     * xxxxUserInfo方法
     */
    public function testxxxxUserInfoData()
    {
        try {
            $ApiInfo = $this->infoArr;

            //set request param
            $parameter = array(
                &#39;in0&#39; => $ApiInfo[&#39;err_msg&#39;],
                &#39;in1&#39; => $ApiInfo[&#39;err_code&#39;],
                &#39;in2&#39; => $ApiInfo[&#39;date&#39;]
            );

            $result = $this->getSoapClientHandler()->synchUserInfo($parameter);

            //调用结果返回异常
            if (!$result instanceof stdClass) {
                throw new Exception("调用synchUserInfo结果出现异常:" . json_encode($result));
            }

            //调用接口状态码,输出对应错误详情
            if ($result->out == &#39;01&#39;) {
                throw new Exception("调用synchUserInfo=>error:" . $result->out . ",msg:接口数据异常");
            }

            $xml_parser = xml_parser_create();
            if (!xml_parse($xml_parser, $result->out, true)) {
                xml_parser_free($xml_parser);
                throw new Exception("调用synchUserInfo返回的不是一个xml结构体");
            }
            xml_parser_free($xml_parser);
            //XXE
            libxml_disable_entity_loader(true);
            $xml = simplexml_load_string($result->out, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
            // 输出参数
            var_dump($xml->data);
            echo " 成功".PHP_EOL;
        } catch (SoapFault $soapFault) {
            throw new Exception($soapFault->getMessage() . $this->getSoapClientHandler()->__getLastResponse());
        }
    }

    /**
     * @description getSoapClientHandler
     */
    public function getSoapClientHandler()
    {
        if (!self::$soapClientHandler) {
            self::$soapClientHandler = new SoapClient($this->getSynchApi());
        }
        return self::$soapClientHandler;
    }

    /**
     * @description getSynchApi
     */
    public function getSynchApi()
    {
        return $this->apiurl;
    }

}
?>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles