cantie/app-store-connect-api

访问 App Store Connect API 的 PHP 客户端库

v1.34.0 2024-03-23 04:07 UTC

This package is auto-updated.

Last update: 2024-09-14 10:08:08 UTC


README

GitHub Release Packagist Downloads Packagist License Packagist Dependency Version

此库可自动化在 App Store Connect 中执行的操作。其客户端基于 Google API PHP Client 修改,我添加了一些 App Store Connect API 的资源。

安装

首选方法是使用 composer。如果您尚未安装 composer,请按照 安装说明 进行操作。

安装 composer 后,在项目根目录中执行以下命令来安装此库

composer require cantie/app-store-connect-api

示例

有关一些 API 的示例,请参阅 examples/ 目录。您可以通过运行内置的 php 网络服务器在浏览器中查看它们。

$ php -S localhost:8000 -t examples/

然后浏览到您指定的主机和端口(在上面的示例中,https://:8000)。

基本示例

use AppleClient;
use AppleService_AppStore;
use Cantie\AppStoreConnect\Services\AppStore\CustomerReviewResponseV1CreateRequest;

$client = new AppleClient();
$client->setApiKey("PATH_TO_API_KEY");
$client->setIssuerId($issuerId);
$client->setKeyIdentifier($keyIdentifier);

$appstore = new AppleService_AppStore($client);
// Get apps by Bundle ID
$results = $appstore->apps->listApps([
    "filter[bundleId]" => "YOUR_BUNDLE_ID" // filter LIKE
]);

// Get all customer reviews for each app
foreach ($results->getData() as $app) {
    $appCustomerReviews = $appstore->apps->listAppsCustomerReviews($app->getId());
    foreach ($appCustomerReviews as $appCustomerReview) {
        // Print all reviewer's nickname
        $appCustomerReview->getAttributes()->getReviewerNickName(), "<br /> \n";

        // Get response for this review
        $customerReviewResponseV1Response = $appstore->customerReviews->getCustomerReviewsResponse($appCustomerReview->getId());

        // Create or update response for this review
        $postBody = new CustomerReviewResponseV1CreateRequest([
            'data' => [
                'attributes' => [
                    'responseBody' => "YOUR_REPLY_TEXT_HERE"
                ],
                'relationships' => [
                    'review' => [
                        'data' => [
                            'id' => $appCustomerReview->getId()
                        ]
                    ]
                ]
            ]
        ]);
        $customerReviewResponseV1Response = $appstore->customerReviewResponses->createCustomerReviewResponses($postBody);

        // Or just delete the response if existed
        $appstore->customerReviewResponses->deleteCustomerReviewResponses($customerReviewResponseV1Response->getData()->getId());
    }
}

创建新客户端

use AppleClient;

$client = new AppleClient();
$client->setApiKey("PATH_TO_API_KEY");
$client->setIssuerId($issuerId);
$client->setKeyIdentifier($keyIdentifier);
// Optional: create new JWT token. If skip this step, token are auto generated when first request are sent
$client->generateToken();

发送请求

对于几乎所有请求(除了上传服务),我们使用 AppStore 服务来处理

use AppleService_AppStore;
// All resources and their methods parameters are listed in src/Service/AppStore.php
$appstore = new AppleService_AppStore($client);
// Make request, for example we call request for an Apps's resources
$appstore->apps->listAppsAppStoreVersions($APP_ID_HERE, $OPTIONAL_PARAMS);

详细信息,请参阅 src/Services/AppStore/Resource/*

别名

基本类已进行别名设置以方便使用,更多详情请参阅 src/aliases.php

$classMap = [
    'Cantie\\AppStoreConnect\\Client' => 'AppleClient',
    'Cantie\\AppStoreConnect\\Service' => 'AppleService',
    'Cantie\\AppStoreConnect\\Services\\AppStore' => 'AppleService_AppStore',
    'Cantie\\AppStoreConnect\\Services\\Upload' => 'AppleService_Upload'
];

将资产上传到 App Store Connect

在这个示例中,我们将一个截图文件上传到应用截图集

// Firstly, we get app screenshot set step by step, we can reduce steps by include[] parameters in query
use AppleService_Upload;
use Cantie\AppStoreConnect\Services\AppStore\AppScreenshotCreateRequest;
use Cantie\AppStoreConnect\Services\AppStore\AppScreenshotUpdateRequest;

$appId = $app->getId(); // $app from previous example
$appStoreVersions = $appstore->apps->listAppsAppStoreVersions($appId);
// Get first app store version id;
$appStoreVersionId = $appStoreVersions->getData()[0]->getId();
// Get list localizations of this version
$appStoreVersionLocalizations = $appstore->appStoreVersions->listAppStoreVersionsAppStoreVersionLocalizations($appStoreVersionId);
// Get first localization id
$appStoreVersionLocalizationId = $appStoreVersionLocalizations->getData()[0]->getId();
// Get list app screenshot sets for this localization
$appScreenshotSets = $appstore->appStoreVersionLocalizations->listAppStoreVersionLocalizationsAppScreenshotSets($appStoreVersionLocalizationId);
// Get first set id
$appScreenshotSetId = $appScreenshotSets->getData()[0]->getId();

// Now, we make an asset reservation
$fileName = "YOUR_FILE_NAME";
$filePath = "FULL_PATH_TO_YOUR_FILE" . $fileName;
$requestCreateAppScreenshot = new AppScreenshotCreateRequest([
    'data' => [
        'type' => 'appScreenshots',
        'attributes' => [
            'fileSize' => filesize($filePath),
            'fileName' => $fileName
        ],
        'relationships' => [
            'appScreenshotSet' => [
                'data' => [
                    'type' => 'appScreenshotSets',
                    'id' => $appScreenshotSetId
                ]
            ]
        ]
    ]
]);
// Create new app screenshot
$appScreenshot = $appstore->appScreenshots->createAppScreenshots($requestCreateAppScreenshot);
$appScreenshotId = $appScreenshot->getData()->getId();
// Follow instruction from UploadOperation[] return in $appScreenshot to upload part or whole asset file
// We can upload parts of your asset concurrently
foreach ($appScreenshot->getData()->getAttributes()->getUploadOperations() as $uploadOperation) {
    $upload = new AppleService_Upload($client, $uploadOperation); // $client from above example
    $ret = $upload->uploadAssets->upload($uploadOperation, $filePath);
}
// Finally, commit the reservation
$appScreenshotUpdateRequest = new AppScreenshotUpdateRequest([
    'data' => [
        'type' => 'appScreenshots',
        'id' => $appScreenshotId,
        'attributes' => [
            'sourceFileChecksum' => md5_file($filePath),
            'uploaded' => true
        ]
    ]
]);
$ret = $appstore->appScreenshots->updateAppScreenshots($appScreenshotId, $appScreenshotUpdateRequest); 

初始化类

所有对象类都从 Model.php 继承,可以通过属性名称和值的数组进行初始化,如前一个示例

use Cantie\AppStoreConnect\Services\AppStore\AppScreenshotUpdateRequest;
$appScreenshotUpdateRequest = new AppScreenshotUpdateRequest([
    'data' => [
        'type' => 'appScreenshots',
        'id' => $appScreenshotId,
        'attributes' => [
            'sourceFileChecksum' => md5_file($filePath),
            'uploaded' => true
        ]
    ]
]);

缓存

JWT 令牌缓存 10 分钟,并且只有在不存在或已过期时才会创建。JWT 令牌不会在客户端之间共享。每个客户端都有自己的令牌,如 src/Client.php 中定义

public function generateToken()
{
    $tokenGenerator = new Generate($this->getApiKey(), $this->getKeyIdentifier(), $this->getIssuerId());
    $jwtToken = $tokenGenerator->generateToken();
    // cache for 10 minutes
    $this->jwtToken = $jwtToken;
    $this->jwtTokenExpTime = Carbon::now()->addMinutes(10)->timestamp;
    return $jwtToken;
}