Skip to content

Commit 93e8608

Browse files
committed
Use composer api to retrieve the package metadata
1 parent 87985d0 commit 93e8608

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/api/package-metadata.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)