PHP code example of gautammkgarg / psr-for-wordpress

1. Go to this page and download the library: Download gautammkgarg/psr-for-wordpress 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/ */

    

gautammkgarg / psr-for-wordpress example snippets


use GautamMKGarg\PsrForWordPress\Http\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new Psr18Client();
$factory = new Psr17Factory();

$request = $factory->createRequest('GET', 'https://api.example.com/endpoint');
$response = $client->sendRequest($request);

echo $response->getStatusCode();    // 200
echo (string) $response->getBody(); // JSON response body

$client = new Psr18Client([
    'timeout'     => 30,   // seconds (default: 10)
    'redirection' => 0,    // disable redirects
    'sslverify'   => false, // disable SSL verification (not recommended in production)
]);

add_filter('http_request_args', function(array $args, string $url): array {
    // Increase timeout for payment gateway API calls
    $args['timeout'] = 30;
    return $args;
}, 10, 2);

use GautamMKGarg\PsrForWordPress\Cache\WpTransientsCache;
use GautamMKGarg\PsrForWordPress\Cache\WpObjectCache;

// Persistent cache (survives requests)
$cache = new WpTransientsCache();

// In-memory cache (request-only unless Redis plugin)
$cache = new WpObjectCache('myplugin');

$cache->set('users', $users, 3600); // seconds
$users = $cache->get('users');
$cache->delete('users');