PHP code example of shewa12 / wp-http-client

1. Go to this page and download the library: Download shewa12/wp-http-client 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/ */

    

shewa12 / wp-http-client example snippets


use Shewa\WP_HTTP_Client\HTTPClient;

// Instantiate the HTTPClient class
$http_client = new HTTPClient();

$url = 'https://api.example.com/data';
$response = $http_client->request( 'get', $url );

if ( ! is_wp_error( $response ) ) {
	// Process the response data
	print_r( $response );
} else {
	// Handle the error
	echo 'Error: ' . $response->get_error_message();
}

$url = 'https://api.example.com/data';
$data = ['name' => 'John'];
$response = $http_client->request( 'post', $url, $data );

if ( ! is_wp_error( $response ) ) {
	// Process the response data
	print_r( $response );
} else {
	// Handle the error
	echo 'Error: ' . $response->get_error_message();
}

$response = array(
	'headers' => [],
	'code'    => 200,
	'message' => 'OK',
	'body'    => [],
);

$defaults = array(
	'method'              => 'GET',
	'timeout'             => 5,
	'redirection'         => 5,
	'httpversion'         => 1.0,
	'user-agent'          => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' )',
	'reject_unsafe_urls'  => false,
	'blocking'            => true,
	'headers'             => array(),
	'cookies'             => array(),
	'body'                => null,
	'compress'            => false,
	'decompress'          => true,
	'sslverify'           => true,
	'sslcertificates'     => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
	'stream'              => false,
	'filename'            => null,
	'limit_response_size' => null,
);