GithubHelp home page GithubHelp logo

icemont / curlwrapper Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 15 KB

The simplest and smallest OOP wrapper for PHP cURL with no overhead.

License: MIT License

PHP 100.00%
curl curl-wrapper curlwrapper php api oop

curlwrapper's Introduction

CurlWrapper

Latest Stable Version PHP Version Require License

The simplest and smallest OOP wrapper for PHP cURL with no overhead.
Designed to test and work with simple RestFull and JSON APIs and execute simple queries.

Installation

$ composer require icemont/curlwrapper

Usage

Example POST request with custom header and data

File: examples/simple_request.php

use Icemont\cURL\CurlWrapper;

$curl = new CurlWrapper();

/*
 * Preparing request
 */

$curl->setTimeout(5)
     ->setUserAgent('Mozilla/5.0 (compatible; CurlWrapper/2.0)')
     ->setReferer('https://example.com/')
     ->addHeader('API-Key: TEST_KEY')
     ->addData('test', 'value')
     ->addData('param2', 'value2')
     ->addDataFromArray([
        'fromArray1' => 'valueA',
        'fromArray2' => 'valueB',
    ]);

/**
 * Executing the query
 */
var_dump($curl->postRequest('https://httpbin.org/post'));

echo 'Request response code: ' . $curl->getLastCode() . PHP_EOL;
echo 'Request error string: ' . $curl->getLastError() . PHP_EOL;

Simple POST and GET requests

use Icemont\cURL\CurlWrapper;

$curl = new CurlWrapper();

// Executing an empty POST request
var_dump($curl->postRequest('https://httpbin.org/post'));

// Add data and execute a POST request with data sending
$curl->addData('value', 'test')
     ->addData('value2', 'test2');
var_dump($curl->postRequest('https://httpbin.org/post'));


// Execute a simple GET request (data will be added as Query String)
var_dump($curl->getRequest('https://httpbin.org/get'));

// Reset the data and execute a simple GET request again
$curl->reset();
var_dump($curl->getRequest('https://httpbin.org/get'));

Example of new ticket creation via the osTicket API

File: examples/osticket_create_ticket.php

use Icemont\cURL\CurlWrapper;

$api = new CurlWrapper();

/**
 * Adding data one by one
 */
$api->addHeader('X-API-Key: YOUR_API_KEY')
    ->addData('source', 'API')
    ->addData('name', 'Test User')
    ->addData('email', '[email protected]')
    ->addData('ip', '127.0.0.1');

/**
 * Or immediately in the array
 */
$api->addDataFromArray([
    'alert' => true,
    'autorespond' => true,
    'subject' => 'Testing API',
    'message' => 'MESSAGE HERE',
]);

/**
 * Executing the query as JSON
 */
var_dump($api->jsonRequest('https://support.example.com/api/tickets.json'));

echo 'Request response code: ' . $api->getLastCode() . PHP_EOL;
echo 'Request error string: ' . $api->getLastError() . PHP_EOL;

Contact

Open an issue on GitHub if you have any problems or suggestions.

License

The contents of this repository is released under the MIT license.

curlwrapper's People

Contributors

icemont avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.