PHP code example of valmaraz / php-network

1. Go to this page and download the library: Download valmaraz/php-network 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/ */

    

valmaraz / php-network example snippets


use vAlmaraz\network\Network;

// Set URL and execute request
$response = Network::get($url)->execute();

// Retrieve response
echo $response->getBody();

use vAlmaraz\network\Network;

$timeoutInSeconds = 3;
$headers = ['Accept' => 'application/json'];
$formData = ['field1' => 'value1', 'field2' => 'value2'];

// Configure request
$response = Network::post($url)
    ->setTimeoutInSeconds($timeoutInSeconds)
    ->withHeaders($headers)
    ->withFormData($formData)
    ->execute();

// Retrieve response info
echo $response->getStatusCode();
echo json_encode($response->getHeaders());
echo $response->getBody();

use vAlmaraz\network\Network;

$network = new Network();
// GET
$response = $network->get('https://jsonplaceholder.typicode.com/posts')
    ->execute();
// POST
$response = $network->post('https://jsonplaceholder.typicode.com/posts')
    ->withFormData(['title' => 'My title', 'body' => 'The body', 'userId' => 123])
    ->execute();
// PATCH
$response = $network->patch('https://jsonplaceholder.typicode.com/posts/1')
    ->withFormData(['title' => 'A new title'])
    ->execute();
// PUT
$response = $network->put('https://jsonplaceholder.typicode.com/posts/1')
    ->withFormData(['title' => 'My title', 'body' => 'The body', 'userId' => 123])
    ->execute();
// DELETE
$response = $network->delete('https://jsonplaceholder.typicode.com/posts/1')
    ->execute();

echo 'Status code: ' . $response->getStatusCode() . PHP_EOL . PHP_EOL;
echo 'Headers: ' . json_encode($response->getHeaders()) . PHP_EOL . PHP_EOL;
echo 'Body: ' . $response->getBody();

composer