https://github.com/activ8-developers/purli
Purli (PHP Url Interface) is lightweight library with object-oriented interface for sending HTTP requests.
https://github.com/activ8-developers/purli
api curl http httpclient php purli socket webservices
Last synced: 2 months ago
JSON representation
Purli (PHP Url Interface) is lightweight library with object-oriented interface for sending HTTP requests.
- Host: GitHub
- URL: https://github.com/activ8-developers/purli
- Owner: ACTIV8-Developers
- License: mit
- Created: 2016-07-30T14:11:37.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2023-06-18T10:32:37.000Z (almost 2 years ago)
- Last Synced: 2025-03-22T14:45:22.907Z (2 months ago)
- Topics: api, curl, http, httpclient, php, purli, socket, webservices
- Language: PHP
- Homepage:
- Size: 34.2 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Purli
=
[](http://opensource.org/licenses/MIT)
[](https://github.com/Kajna/Purli/releases)Purli (PHP Url Interface) is the lightweight library with the object-oriented interface for sending HTTP requests.
Installing
=
This package is available via [Composer](https://getcomposer.org/):```json
{
"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.
```php
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
```php
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
```php
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
```php
try {
$data = 'bar';$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
```php
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
```php
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
```php
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](https://phpunit.de/) for testing, navigate to project root directory and run command:
```$xslt
cd tests
phpunit
```Author
=
Milos Kajnaco
[email protected]Contributors
=
Nemanja Nikolic
[email protected]Licence
=
Purli is released under the [MIT](http://opensource.org/licenses/MIT) public license.