Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/api/package-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Reading package metadata via API

To read the package metadata via API, use the Composer API on your Private Packagist repository.

This is useful when you need to programmatically access package metadata beyond what's available through the Private Packagist API
(for example to read custom configuration from the `extra` section of a package's composer.json).

Read more about the API: https://packagist.org/apidoc#get-package-data


### Example implementation

The process works as follows:
1. Get the JSON from `https://repo.packagist.com/<org>/p2/<package>.json`
2. Authenticate using username `token` and an Authentication Token as password
3. Decode the JSON as nested arrays
4. Use [MetadataMinifier](https://github.com/composer/metadata-minifier) to expand the metadata for each package version: `$metadata = MetadataMinifier::expand($data['packages'][$package])`

```bash
# Make sure the composer/metadata-minifer package is installed:
composer require composer/metadata-minifier
```

```php
use Composer\MetadataMinifier\MetadataMinifier;

// Assuming your organization name is acme-company and the package name is acme/package
$organization = 'acme-company';
$package = 'acme/package';

// NOTE: You must use an authentication token for the composer API, not the Private Packagist API credentials.
// You can create a token in your Organization settings > Authentication Tokens
$token = 'packagist_ort_..........';

// The p2/$vendor/$package.json endpoint contains only tagged releases. If you want to fetch information about branches (i.e. dev versions)
// you need to use p2/$vendor/$package~dev.json.
$apiUrl = sprintf('https://repo.packagist.com/%s/p2/%s.json', $organization, $package);

$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'token:' . $token
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we want to spend more time on this, but generally prefer to teach people good ideas in docs, e.g. using symfony http client rather than plain curl? But guess enough people will want to use this 🤦

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry ignore me. Really got more important things to do sorry for the distraction, we can revisit this at another point and hopefully people work this out themselves :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be nicer, but I guess non-symfony devs might prefer seeing something more general :)
Maybe switching the example to guzzle would be a good compromise?


$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
$metadata = MetadataMinifier::expand($data['packages'][$package]);
```