|
| 1 | +# Reading package metadata via API |
| 2 | + |
| 3 | +To read the package metadata via API, use the Composer API on your Private Packagist repository. |
| 4 | + |
| 5 | +Read more about the API: https://packagist.org/apidoc#get-package-data |
| 6 | + |
| 7 | +### Example implementation |
| 8 | + |
| 9 | +```bash |
| 10 | +# Make sure the composer/metadata-minifer package is installed: |
| 11 | +composer require composer/metadata-minifier |
| 12 | +``` |
| 13 | + |
| 14 | +```php |
| 15 | +use Composer\MetadataMinifier\MetadataMinifier; |
| 16 | + |
| 17 | +// Assuming your organization name is acme-company and the package name is acme/package |
| 18 | +$organization = 'acme-company'; |
| 19 | +$package = 'acme/package'; |
| 20 | + |
| 21 | +// NOTE: You must use an authentication token for the composer API, not the Private Packagist API credentials. |
| 22 | +// You can create a token in your Organization settings > Authentication Tokens |
| 23 | +$token = 'packagist_ort_..........'; |
| 24 | + |
| 25 | +$apiUrl = sprintf('https://repo.packagist.com/%s/p2/%s.json', $organization, $package); |
| 26 | + |
| 27 | +$ch = curl_init($apiUrl); |
| 28 | +curl_setopt_array($ch, [ |
| 29 | + CURLOPT_RETURNTRANSFER => true, |
| 30 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
| 31 | + CURLOPT_USERPWD => 'token:' . $token |
| 32 | +]); |
| 33 | + |
| 34 | +$response = curl_exec($ch); |
| 35 | +curl_close($ch); |
| 36 | + |
| 37 | +$data = json_decode($response, true); |
| 38 | +$metadata = MetadataMinifier::expand($data['packages'][$package]); |
| 39 | +``` |
| 40 | + |
| 41 | + |
0 commit comments