2017 © Pedro Peláez
 

library purli

Lightweight library with Object-Oriented interface for sending HTTP requests

image

kajna/purli

Lightweight library with Object-Oriented interface for sending HTTP requests

  • Wednesday, November 8, 2017
  • by Kajna
  • Repository
  • 2 Watchers
  • 0 Stars
  • 64 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Purli

DUB Version, (*1)

Purli (PHP Url Interface) is the lightweight library with the object-oriented interface for sending HTTP requests., (*2)

Installing

This package is available via Composer:, (*3)

{
  "require": {
    "kajna/purli": "dev-master"
  }
}

Usage examples

Fetching data using GET method and CURL handler

Minimal example, Purli by default uses CURL handler if available otherwise fallback to the socket., (*4)

try {
    $purli = (new \Purli\Purli())
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

Fetching data using GET method and socket handler

If explicitly set Purli will use PHP sockets to make requests regardless if CURL is installed or not, (*5)

try {
    $purli = (new \Purli\Purli(\Purli\Purli::SOCKET))
            ->get('http://example.com')
            ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

Fetching data using POST method

try {
    $data = array('foo' => 'bar');

    $purli = (new \Purli\Purli())
        ->setParams($data)
        ->post('http://www.example.com')
        ->close();

    $response = $purli->response();

    print_r($response->asText());
} catch(\Exception $e) {
    echo $e->getMessage();
}

Sending and receiving XML data using POST method

try {
    $data = '<root><foo>bar</foo></root>';

    $purli = (new \Purli\Purli())
        ->setUserAgent('curl 7.16.1 (i386-portbld-freebsd6.2) libcurl/7.16.1 OpenSSL/0.9.7m zlib/1.2.3')
        ->setHeader('Content-Type', 'text/xml')
        ->setParams($data)
        ->post('http://www.example.com')
        ->close();

    $response = $purli->response();

    print_r($response->asArray());
} catch(\Exception $e) {
    echo $e->getMessage();
}

Sending and receiving JSON data using PUT method

try {
    $data = array('foo' => 'bar');
    $json = json_encode($data);

    $purli = (new \Purli\Purli(\Purli\Purli::SOCKET))
            ->setConnectionTimeout(3)
            ->setHeader('Content-Type', 'application/json')
            ->setParams($json)
            ->put('http://www.example.com')
            ->close();

    $response = $purli->response();

    print_r($response->asObject());
} catch(\Exception $e) {
    echo $e->getMessage();
}

Using proxy server to make request

try {
    $purli = (new \Purli\Purli());

    $purli
        ->setProxy(PROXY_ADDRESS, PROXY_PORT)
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

Setting custom CURL option

If CURL extension is installed by default Purli will use it, you can always get CURL handler object and set custom option if more flexibility is needed, (*6)

try {
    $purli = (new \Purli\Purli());

    if ($purli->getHandlerType() === \Purli\Purli::CURL) {
        curl_setopt($purli->getHandler(), CURLOPT_TIMEOUT, 10);
    }

    $purli
        ->get('http://www.example.com')
        ->close();

    $response = $purli->response();

    echo $response->asText();
} catch(\Exception $e) {
    echo $e->getMessage();
}

Running tests

Purli uses PHPUnit for testing, navigate to project root directory and run command: $xslt cd tests phpunit, (*7)

Author

Milos Kajnaco milos@caenazzo.com, (*8)

Contributors

Nemanja Nikolic nemanja@massvision.net, (*9)

Licence

Purli is released under the MIT public license., (*10)

The Versions

08/11 2017

dev-master

9999999-dev

Lightweight library with Object-Oriented interface for sending HTTP requests

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Milos Kajnaco

curl https json xml rest http socket

08/11 2017

1.0.0

1.0.0.0

Lightweight library with Object-Oriented interface for sending HTTP requests

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Milos Kajnaco

curl https json xml rest http socket

25/09 2016

1.0.0-rc

1.0.0.0-RC

Lightweight library with Object-Oriented interface for sending HTTP requests

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Milos Kajnaco

curl https json xml rest http socket