PHP code example of knplabs / packagist-api

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

    

knplabs / packagist-api example snippets


use Packagist\Api\Client;

$client = new Client();

foreach ($client->search('sylius') as $result) {
    echo $result->getName() . PHP_EOL;
}

// sylius/sylius
// sylius/resource-bundle
// sylius/grid-bundle
// ...

$client->search('cart', ['type' => 'symfony-bundle']);

$client->search('sylius', limit: 2);

$package = $client->get('sylius/sylius');

echo "{$package->getName()}: {$package->getDescription()}";

// sylius/sylius: E-Commerce platform for PHP, based on Symfony framework.

$package = $client->getComposer('sylius/sylius')['sylius/sylius'];
$latest = $package->getVersions()[0];

echo "{$latest->getName()} {$latest->getVersion()}";

// sylius/sylius v2.3.1

$releases = $client->getComposerReleases('sylius/sylius');
$branches = $client->getComposerBranches('sylius/sylius');

foreach ($client->all() as $name) {
    echo $name . PHP_EOL;
}

// 0.0.0/composer-

$client->all(['type' => 'library']);
$client->all(['vendor' => 'sylius']);

$client->all([
    'vendor' => 'sylius',
    'fields' => ['abandoned', 'repository', 'type'],
]);

// [
//     'sylius/addressing' => [
//         'abandoned' => false,
//         'repository' => 'https://github.com/Sylius/Addressing',
//         'type' => 'library',
//     ],
//     ...
// ]

foreach ($client->popular(3) as $result) {
    echo $result->getName() . PHP_EOL;
}

// symfony/deprecation-contracts
// symfony/polyfill-mbstring
// psr/log

$advisories = $client->advisories(['monolog/monolog']);

foreach ($advisories['monolog/monolog'] ?? [] as $advisory) {
    echo "{$advisory->getTitle()} ({$advisory->getAffectedVersions()})" . PHP_EOL;
}

$client->advisories(['monolog/monolog' => '1.8.1'], filterByVersion: true);

// Header injection in NativeMailerHandler (>=1.8.0,<1.12.0)

$client->advisories(['monolog/monolog'], updatedSince: strtotime('2024-01-01'));

$client->setPackagistUrl('https://custom.packagist.site.org');

// Or in the constructor:
$client = new Client(packagistUrl: 'https://custom.packagist.site.org');

use GuzzleHttp\Client as GuzzleClient;
use Packagist\Api\Client;

$client = new Client(new GuzzleClient(['timeout' => 5]));