PHP code example of neon-php / simple-http

1. Go to this page and download the library: Download neon-php/simple-http library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neon-php / simple-http example snippets




use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');



use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');
$response = Http::post('http://example.com/api');
$response = Http::put('http://example.com/api');
$response = Http::patch('http://example.com/api');
$response = Http::delete('http://example.com/api');



use Neon\Http\Facade\Http;

Http::setBaseURL('http://example.com');

$response = Http::get('/api');



use Neon\Http\Facade\Http;

Http::setBaseURL('http://example.com');
Http::setFrameworkMethod(true);

$response = Http::put('/api');



use Neon\Http\Facade\Http;

$response = Http::addParam('key', 'value')->post('/api');



use Neon\Http\Facade\Http;

$response = Http::post('/api', [
    'some_key' => 'some_value',
    'another_key' => 'another_value'
]);



use Neon\Http\Facade\Http;

$response = Http::post('/api', [
    'query' => [
        'some_key' => 'some_value',
        'another_key' => 'another_value'
    ]
]);

// Resulting request url:
// http://example.com/api?some_key=some_value&another_key=another_value



use Neon\Http\Facade\Http;

$response = Http::addHeader('Accept', 'application/json')->post('/api');



use Neon\Http\Facade\Http;

$response = Http::bearer($token)->post('/api');



use Neon\Http\Facade\Http;

$image = $_FILES['image'];

$response = Http::file('image', $image['name', $image['tmp_name']])->post('/api');



use Neon\Http\Facade\Http;

$response = Http::get('http://example.com/api');

if ($response->hasHeader('Content-Length')) {
    // Header Content-Length exists
}



$values = $response->getHeader('Content-Length');



$code = $response->code();



// Status code is 200, 201 or 204
$response->successfull();

// Status code is bigger than or equal to 400
$response->failure();

// Status code is bigger than or equal to 500
$response->serverError();

// Status code is bigger than or equal to 400 AND less than 500
$response->clientError();

// Status code is 201
$response->created();

// Status code is 204
$response->noContent();

// Status code is 404
$response->notFound();

// Status code is 401
$response->unauthorized();

// Status code is 403
$response->forbidden();

// Status code is 400
$response->badRequest();



$body = $response->body();



$body_stream = $response->bodyRaw();



use Neon\Http\Exceptions\RequestException;

try {
    $body = $response->json();
} catch (RequestException $e) {
    // Response body is not json
}