PHP code example of medianet-dev / p-connector

1. Go to this page and download the library: Download medianet-dev/p-connector 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/ */

    

medianet-dev / p-connector example snippets


# file: p-connector.php

/*
|--------------------------------------------------------------------------
| List of the available profiles with it's configurations
|--------------------------------------------------------------------------
*/
'profiles' => [
    'demo' => [
        'protocol' => 'https',
        'host' => 'my-json-server.typicode.com',
        'port' => 443,
        'prefix' => 'typicode/demo',
    ],
],

// Import the PConnector facade to get available methods hints 
use MedianetDev\PConnector\Facade\PConnector;

// Or use tha alias without available methods hints
# use PConnector;

// Method 1:
$demo = PConnector::get('posts/1');
echo '<h1>'.$demo->title.'</h1>';

// Method 2:
# $demo = PConnector::send('posts/1', [], 'GET');
# echo '<h1>'.$demo->title.'</h1>';

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::profile('demo')->get('posts/1');
$demo2 = PConnector::profile('demo_2')->get('users/1');

send(string $path = '', array $data = [], string $method = 'GET')
post(string $path = '', array $data = [])
get(string $path = '', array $data = [])
put(string $path = '', array $data = [])
patch(string $path = '', array $data = [])
delete(string $path = '', array $data = [])

# file: p-connector.php

'auth' => [ // [AAPS]
    // Whether to use authentication by default or not (you can make an exception with withAuth() and withoutAuth() methods)
    'authenticate_by_default' => false,
    // How to authenticate (Accepted values are: api_key, basic, bearer)
    'auth_method' => 'bearer',
    // If the api_key method is use then you SHOULD provide an api_key key
    // 'api_key' => 'X-AUTH-TOKEN',
    // The path of the login
    'login_path' => 'login',
    // The http method used to login
    'login_http_method' => 'POST',
    'credentials' => [
        'username' => 'username',
        'password' => 'password',
    ],
    // The expected http code response when login in
    'success_login_code' => [200],
    // When should we re-authenticate
    're_auth_on_codes' => [401],
    // Where to find the token in the response (you can youse the dot syntax EX: 'data.user.auth.token')
    'token_path' => 'token',
],

'profiles' => [
    'demo' => [
        ...,
        'auth' => [ // Add this section for custom auth settings
            // Authentication settings specific to this profile
        ],
    ],
],

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::profile('demo')->withoutAuth()->get('posts/1');
# $demo = PConnector::withoutAuth()->get('users/1');
 
# file: p-connector.php

// Request settings
'request' => [ // [AAPS]
    // The default request headers
    'headers' => ['Accept' => 'application/json'],
    // Whether to send the language through the header by default or not
    'enable_localization' => true,
    // Handle http errors or not
    'http_errors' => false,
    // Http connect timeout value
    'connect_timeout' => 3,
    // Http timeout value
    'timeout' => 3,
    // The data type; json, form-data, ...
    'post_data' => 'json',
],

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::lang('ar')->get('posts/1');

PConnector::url('https://jsonplaceholder.typicode.com')->get('todos')

# file: p-connector.php

'decode_response' => 'object', // [AAPS]

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::objectResponse()->get('posts/1');
$demo = PConnector::htmlResponse()->get('posts/1');
$demo = PConnector::arrayResponse()->get('posts/1');

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->getResponseBody();

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->getAttribute('title');
$demo = PConnector::get('posts/1')->title;
$demo = PConnector::get('posts/1')->getAttribute('author.name', 'Unknown author');

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1');
if ($demo->responseCodeNot(200)) {
    echo 'This is not what I was expecting from Demo!';
} else {
    echo '<h1>'.$demo->title.'</h1>';
}

$demo = PConnector::post('posts', ['title' => 'My title']);
 if ($demo->responseCodeNotIn([200, 201])) {
    echo 'This is not what I was expecting from Demo!';
} else {
    echo 'Post created, Hola!';
}

# file: p-connector.php

// Log requests & responses to log files or not (you can make an exception with withLog() and withoutLog() methods)
'log' => false, // [AAPS]

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->dump();

if ($demo->responseCodeNot(200)) {
    $demo->log();
} else {
    echo '<h1>'.$demo->title.'</h1>';
}

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::withLog()->get('posts/1');

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->dump();
$demo = PConnector::get('posts/1')->dd();
bash
php artisan migrate
bash
php artisan vendor:publish --provider="MedianetDev\PConnector\PConnectorServiceProvider"