定制 myoperator/transport 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

myoperator/transport

Composer 安装命令:

composer require myoperator/transport

包简介

Guzzle wrapper for making GET or POST or any network requests

README 文档

README

Travis (.org) branch Travis (.org) branch

MyOperator Transport

This is a package that uses guzzle http transport and can be used to make network requests. This internally uses Guzzle library to make requests.

Quick Start

To make a GET or POST requests:

include_once 'vendor/autoload.php';

use MyOperator\Transport\Transport;

$transport = new Transport('http://localhost/api');

// Making a simple GET request
$response = $transport->get('/users?a=b&c=d');

// More better GET request
$response = $transport->get('/users', ['a' => 'b', 'c' => 'd']); 
// Equivalent to curl -XGET -H 'application/json' http://localhost/api/users?a=b&c=d

// To make a POST
$response = $transport->post('/users', ['a' => 'b']); 
// Equivalent to curl -XPOST -d a=b -H 'application/json' http://localhost/api/users

// Response can be directly cast to string
echo (string) $response; // {"a" : "b"}

//Or json if you like
print_r($response->json()); // ['a' => 'b']

// Or plaintext, same like casting
echo $response->text(); //{"a": "b"}

Using Responses

Responses are the main part of making any responses. Since responses can be of any type (i.e. json, xml, text etc) this library takes cares of automatically converting any json encodeable responses.

Responses are part of MyOperator\Transport\Response. Hence any response have three available methods:

  • json() which returns array if response is valid json. Else the response is returned as is
  • text() returns the text response as is
  • getStatus() returns the response HTTP Status code
//Assuming webservice `/getjson` returns {'a': 'b'}
// and '/gettext' returns 'Simple text response'

$response = $transport->get('/getjson');
// assertTrue $response->json() == ['a' => 'b']
// assertTrue $response->text() == '{"a": "b"}'

$response = $transport->get('/gettext');
// assertTrue $response->json() == 'Simple text response'
// assertTrue $response->text() == 'Simple text response'
// assertTrue (string) $response == 'Simple text response'

Setting headers

Sometime, you may wish to add headers, which can be easily done using setHeaders method.

use MyOperator\Transport\Transport;

$transport = new Transport('http://localhost');

$transport->setHeaders(['X-Auth-key' => 'xyz']);

$transport->post('/login');

OAuth

Many uses OAuth in day to day process. This library takes care of refreshing your access token. Please note that this library does not generate your access token but only refreshes it automatically, if some unauthorized status code is encountered.

Please read more here: Oauth.md

Mocking network requests

This library aims at making writing unit tests and mocks a breeze. This library provides a fluent Guzzle mock api to make mocking easy.

To mock a network request, you can easily create a mock using MyOperator\Transport\TransportMock. Then can you queue some responses to it. You can then call your apis and the mock will replay the queues responses in order.

For instance, to mock a 200 SUCCESS response from any api, you can do:

use MyOperator\Transport\TransportMock;

// Inititalising a mocker
$transport = new TransportMock();

//Creating custom response. order is createResponse($body, $headers= [], $status_code=200);
$mockResponse = $transport->createResponse(json_encode(['a' => 'b']));

// Creating another GuzzleHttp\Psr7\Response responses
$anotherResponse = new GuzzleHttp\Psr7\Response(201, [], json_encode(['c' => 'd']));

// Queue a valid GuzzleHttp\Psr7\Response
$transport->queue($mockResponse);
$transport->queue($anotherResponse);

// Finally we can start mock
$transport->mock();

// This will return first queued response, doesn't matter whatever the request is
$response = $transport->get('/get');
//assertTrue $response->json() == ['a' => 'b']
//assertTrue $response->getStatus() == 200

$response = $transport->post('/somethingelse');
//assertTrue $response->json() == ['c' => 'd']
//assertTrue $response->getStatus() == 201

// Since at this point we have exhaused our queued response,
// this will throw an \OutOfBoundsException
$response = $transport->get('/status');

TODO

  • Handling public and private APIs
  • Setting basic authentication process

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2019-09-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固