PHP code example of edmundluong / php-api-client

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

    

edmundluong / php-api-client example snippets




use Edmund\PhpApiClient\AbstractApiDescription;

class TwitterUrlsApiDescription extends AbstractApiDescription
{
    public function load()
    {
        return new static([
            'additionalProperties' => true,
            'baseUrl'              => 'http://urls.api.twitter.com/1/urls/',
            'operations'           => [
                'count' => [
                    'httpMethod'    => 'GET',
                    'uri'           => 'count.json',
                    'responseModel' => 'JsonResponse',
                    'parameters'    => [
                        'url' => [
                            'type'     => 'string',
                            'location' => 'query',
                            '



use Edmund\PhpApiClient\AbstractApiClient;

class TwitterUrlsApiClient extends AbstractApiClient
{
    protected $apiDescription = TwitterUrlsApiDescription::class;
}



$twitterUrls = new TwitterUrlsApiClient();

$response = $twitterUrls->count(['url' => 'http://www.google.com']);

var_dump($response);



use GuzzleHttp\Event\BeforeEvent;
use Edmund\PhpApiClient\Auth\AbstractAuthenticator;

class TwitterUrlsOauthAuthenticator extends AbstractAuthenticator
{
    function sign(BeforeEvent $event)
    {
        $event->getRequest()->getQuery()->add('access_token', 'token_value');
    }
}



use Edmund\PhpApiClient\AbstractApiClient;

class TwitterUrlsApiClient extends AbstractApiClient
{
    protected $apiDescription = TwitterUrlsApiDescription::class;
    protected $authenticators = [
        'oauth2' => TwitterUrlsOauthAuthenticator::class
    ];
}



$oauthClient = new TwitterUrlsApiClient(['authType' => 'oauth2']);

$response = $oauthClient->count(['url' => 'http://www.google.com']);

// GET http://urls.api.twitter.com/1/urls/count.json?url=http%3A%2F%2Fwww.google.com&access_token=token_value



$oauthClient = new TwitterUrlsApiClient([
    'authType'  => 'oauth2',
    'debug'     => true
]);

$response = $oauthClient->count(['url' => 'http://www.google.com']);



$logger = new \Monolog\Logger('name'); 
$twitterUrls = new TwitterUrlsApiClient([
    'debug'         => true,
    'debugLogger'   => $logger
]);

composer 

array(2) {
  'count' =>
  int(25256927)
  'url' =>
  string(22) "http://www.google.com/"
}