Giter VIP home page Giter VIP logo

wp-requests-psr18-adapter's Introduction

WordPress/Requests PSR-18 Adapter

Latest Version Software License Build Status codecov Total Downloads

Use WordPress/Requests as a PSR-18 HTTP client adapter.

  • Requires PHP 7.2+
  • Supports Requests v1.8+ and v2

Why?

Requests is a HTTP library written in PHP, that lacks of support for PSR-7 and also for PSR-18 because of the compatability with PHP 5.6+.

I've created a PR in Requests to add PSR-7 support but this would add new direct dependencies to Requests. So I created this library as an optional wrapper for Requests. If one day Requests nativly supports PSR-7 and PSR-18, this library might become obsolete.

How to use

Installation with Composer

WordPress/Requests PSR-18 Adapter is available on Packagist and can be installed using Composer.

composer require art4/requests-psr18-adapter

If you want to use WordPress/Requests PSR-18 Adapter in context of a WordPress instance (e.g. in a plugin or theme) you should add "rmccue/requests": "*" as a replace package link. This will prevent composer from installing rmccue/requests two times, leading to fatal errors.

Example composer.json:

{
    "require": {
        "art4/requests-psr18-adapter": "^1.1"
    },
    "replace": {
        "rmccue/requests": "*"
    }
}

Examples

Take a look at the examples directory for more examples.

<?php

// First, include the Composer autoload.php
require_once dirname(__DIR__) . '/vendor/autoload.php';

// Define Requests options
$options = [
    'proxy' => '127.0.0.1:8080',
    'transport' => $customTransport,
    // other Requests options
];

// Create the HTTP client
$httpClient = new \Art4\Requests\Psr\HttpClient($options);

// Create a PSR-7 request and optional set other headers
$request = $httpClient->createRequest('GET', 'http://httpbin.org/get');
$request = $request->withHeader('Accept', 'application/json');

try {
    // Send the request
    $response = $httpClient->sendRequest($request);
} catch (\Psr\Http\Client\ClientExceptionInterface $th) {
    // Handle errors
    throw $th;
}

// Use the PSR-7 Response
var_dump($response->getBody()->__toString());

wp-requests-psr18-adapter's People

Contributors

alpipego avatar art4 avatar catharsisjelly avatar ccrims0n avatar cgdangelo avatar datagutten avatar dd32 avatar dependabot[bot] avatar gelolabs avatar jbroadway avatar jrfnl avatar justinyahin avatar kasperfranz avatar laurentmartelli avatar mbabker avatar ntwb avatar ocean90 avatar ozh avatar patmead avatar qibinghua avatar rmccue avatar schlessera avatar simonhammes avatar skyzyx avatar soulseekah avatar staabm avatar stephenharris avatar yevgenko avatar zancarius avatar zegnat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wp-requests-psr18-adapter's Issues

Requirement of Requests package problematic

I've been getting errors using PhpStan WordPress:

$ phpstan analyse 
Note: Using configuration file /Users/brianhenry/Sites/bh-wp-bitcoin-gateway/phpstan.neon.
Fatal error: Cannot declare class WpOrg\Requests\Autoload, because the name is already in use in /Users/brianhenry/Sites/bh-wp-bitcoin-gateway/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php on line 18703
Script phpstan analyse --memory-limit 1G handling the cs event returned with error code 255

I think there are two ways to address it.

Add documentation to the README:

# Add a `provide` entry to tell Composer not to install Requests.
# brew install jq sponge
cat composer.json | jq ".provide += { \"rmccue/requests\": \"*\" }" | sponge composer.json

main...BrianHenryIE:WP-Requests-PSR18-Adapter:patch-1

Or move "rmccue/requests": "^1.8 || ^2.0" from require to require-dev and add suggest:

"suggest": {
    "rmccue/requests": "Requests is included with WordPress but must be required independently otherwise."
}

main...BrianHenryIE:WP-Requests-PSR18-Adapter:patch-2

I think the latter should work. It's more drastic than the first but more straightforward for consumers of this package.

Fatal error due to missing return types

When I try to run the repo's PHPUnit tests, unchanged, I get:

Fatal error: Declaration of Art4\Requests\Psr\Request::getRequestTarget() must be compatible with Psr\Http\Message\RequestInterface::getRequestTarget(): string in /Users/brianhenry/Sites/WP-Requests-PSR18-Adapter/src/Psr/Request.php on line 115

The problem is that psr/http-message introduced return types in v2.0. And psr/http-client allowed "psr/http-message": "^1.0 || ^2.0" in v1.0.2

Adding a requirement on "psr/http-message": "^1.0" works to fix the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.