项目作者: tarkhov

项目描述 :
Guzzle XML request and response.
高级语言: PHP
项目地址: git://github.com/tarkhov/guzzle-xml.git
创建时间: 2018-01-20T09:33:07Z
项目社区:https://github.com/tarkhov/guzzle-xml

开源协议:MIT License

下载


Guzzle XML

Guzzle XML request and response.

Contents

  1. Compatibility
    1. Version support
  2. Installation
    1. Composer
  3. Usage
    1. Request options
    2. Response
  4. Author
  5. License

Compatibility

Library Version
PHP >=7.2.5
Guzzle >=7.0 and < 8.0
Symfony Serializer >=5.0 and < 6.0

Version support

Guzzle PHP Repo
6.x >=5.5 0.x
7.x >=7.2 1.x

Installation

Composer

  1. composer require tarkhov/guzzle-xml

Usage

Request options

Following example creates POST request with XML body. Option xml accepts an array that is converted to XML document. About array format and how converting works you can read in detail Symfony XmlEncoder.

  1. <?php
  2. use GuzzleHttp\HandlerStack;
  3. use GuzzleHttp\Client;
  4. use GuzzleXml\XmlMiddleware;
  5. $stack = HandlerStack::create();
  6. $stack->push(XmlMiddleware::xml(), 'xml');
  7. $client = new Client(['handler' => $stack]);
  8. $response = $client->post('https://example.com', [
  9. 'xml' => [
  10. 'package' => [
  11. '@language' => 'PHP',
  12. 'name' => 'Guzzle XML',
  13. 'author' => [
  14. '@role' => 'developer',
  15. '#' => 'Alexander Tarkhov',
  16. ],
  17. 'support' => [
  18. 'issues' => 'https://github.com/tarkhov/guzzle-xml/issues',
  19. 'source' => 'https://github.com/tarkhov/guzzle-xml',
  20. ],
  21. ],
  22. ],
  23. ]);

As a result, an xml request will be sent with the header Content-type: text/xml and data with the following content:

  1. <?xml version="1.0"?>
  2. <package language="PHP">
  3. <name>Guzzle XML</name>
  4. <author role="developer">Alexander Tarkhov</author>
  5. <support>
  6. <issues>https://github.com/tarkhov/guzzle-xml/issues</issues>
  7. <source>https://github.com/tarkhov/guzzle-xml</source>
  8. </support>
  9. </package>

Response

Automatically convert your JSON response to XML using middleware.

  1. <?php
  2. use GuzzleHttp\HandlerStack;
  3. use GuzzleHttp\Client;
  4. use GuzzleXml\XmlMiddleware;
  5. $stack = HandlerStack::create();
  6. $stack->push(XmlMiddleware::jsonToXml());
  7. $client = new Client(['handler' => $stack]);
  8. $response = $client->post('https://example.com');
  9. $xml = $response->getBody();
  10. echo $xml;

If you json response is:

  1. {
  2. "package": {
  3. "@language":"PHP",
  4. "name":"Guzzle XML",
  5. "author": {
  6. "@role":"developer",
  7. "#":"Alexander Tarkhov"
  8. },
  9. "support": {
  10. "issues":"https:\/\/github.com\/tarkhov\/guzzle-xml\/issues",
  11. "source":"https:\/\/github.com\/tarkhov\/guzzle-xml"
  12. }
  13. }
  14. }

This will automatically convert to XML like this:

  1. <?xml version="1.0"?>
  2. <package language="PHP">
  3. <name>Guzzle XML</name>
  4. <author role="developer">Alexander Tarkhov</author>
  5. <support>
  6. <issues>https://github.com/tarkhov/guzzle-xml/issues</issues>
  7. <source>https://github.com/tarkhov/guzzle-xml</source>
  8. </support>
  9. </package>

Author

Alexander Tarkhov

License

This project is licensed under the MIT License - see the LICENSE file for details.