wimski/curl

A simple wrapper for cURL to use with DI and OOP

1.0.0 2022-10-19 07:36 UTC

This package is auto-updated.

Last update: 2024-11-19 11:48:35 UTC


README

PHPStan PHPUnit Coverage Status Latest Stable Version

cURL

A simple wrapper for cURL to use with DI and OOP.

Changelog

View the changelog.

Install

composer require wimski/curl

Usage

use Wimski\Curl\CurlResourceFactory;

$curlResourceFactory = new CurlResourceFactory();

$curlResource = $curlResourceFactory->make('https://some-webserver.com/resource-to-request');

$response = $curlResource
    ->setOption(CURLOPT_RETURNTRANSFER, true)
    ->execute();
    
$curlResource->close();

Ideally you would set up a singleton binding for the factory in your framework's container and use DI.

use Wimski\Curl\Contracts\CurlResourceFactoryInterface;

class MyClass
{
    public function __construct(
        protected CurlResourceFactoryInterface $curlResourceFactory,
    ) {
    }
    
    public function getData(): string
    {
        $curlResource = $this->curlResourceFactory->make('https://some-webserver.com/resource-to-request');

        $response = $curlResource
            ->setOption(CURLOPT_RETURNTRANSFER, true)
            ->execute();
            
        $curlResource->close();
        
        return $response;
    }
}