tarkhov/guzzle-xml 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tarkhov/guzzle-xml

最新稳定版本:v1.0.0

Composer 安装命令:

composer require tarkhov/guzzle-xml

包简介

Guzzle XML request and response.

README 文档

README

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

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.

<?php
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client;
use GuzzleXml\XmlMiddleware;

$stack = HandlerStack::create();
$stack->push(XmlMiddleware::xml(), 'xml');
$client = new Client(['handler' => $stack]);
$response = $client->post('https://example.com', [
  'xml' => [
    'package' => [
        '@language' => 'PHP',
        'name'      => 'Guzzle XML',
        'author'    => [
          '@role' => 'developer',
          '#'     => 'Alexander Tarkhov',
        ],
        'support'   => [
            'issues' => 'https://github.com/tarkhov/guzzle-xml/issues',
            'source' => 'https://github.com/tarkhov/guzzle-xml',
        ],
    ],
  ],
]);

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

<?xml version="1.0"?>
<package language="PHP">
   <name>Guzzle XML</name>
   <author role="developer">Alexander Tarkhov</author>
   <support>
      <issues>https://github.com/tarkhov/guzzle-xml/issues</issues>
      <source>https://github.com/tarkhov/guzzle-xml</source>
   </support>
</package>

Response

Automatically convert your JSON response to XML using middleware.

<?php
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client;
use GuzzleXml\XmlMiddleware;

$stack = HandlerStack::create();
$stack->push(XmlMiddleware::jsonToXml());
$client = new Client(['handler' => $stack]);
$response = $client->post('https://example.com');
$xml = $response->getBody();
echo $xml;

If you json response is:

{
   "package": {
      "@language":"PHP",
      "name":"Guzzle XML",
      "author": {
         "@role":"developer",
         "#":"Alexander Tarkhov"
      },
      "support": {
         "issues":"https:\/\/github.com\/tarkhov\/guzzle-xml\/issues",
         "source":"https:\/\/github.com\/tarkhov\/guzzle-xml"
      }
   }
}

This will automatically convert to XML like this:

<?xml version="1.0"?>
<package language="PHP">
   <name>Guzzle XML</name>
   <author role="developer">Alexander Tarkhov</author>
   <support>
      <issues>https://github.com/tarkhov/guzzle-xml/issues</issues>
      <source>https://github.com/tarkhov/guzzle-xml</source>
   </support>
</package>

Author

Alexander Tarkhov

License

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

统计信息

  • 总下载量: 36.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-22