mrt1m/playstation-store-api

PlayStation Store API 的工作简单封装

2.1.2 2024-08-18 13:15 UTC

This package is auto-updated.

Last update: 2024-09-18 13:48:17 UTC


README

1. 前置条件

  • PHP 8.1 或更高版本

2. 安装

可以使用Composer通过运行以下命令来安装 playstation-store-api

composer require mrt1m/playstation-store-api

3. 初始化

使用以下代码创建Client对象

<?php
declare(strict_types=1);

use PlaystationStoreApi\Client;
use GuzzleHttp\Client as HTTPClient;
use PlaystationStoreApi\Enum\CategoryEnum;
use PlaystationStoreApi\Enum\RegionEnum;

require_once __DIR__ . '/../vendor/autoload.php';

const API_URL = 'https://web.np.playstation.com/api/graphql/v1/';

$client = new Client(RegionEnum::UNITED_STATES, new HTTPClient(['base_uri' => API_URL, 'timeout' => 5]));

4. API请求

4.1. 通过ID请求产品数据

use PlaystationStoreApi\Request\RequestProductById;

/**
 * Example for https://store.playstation.com/en-us/product/UP0001-CUSA09311_00-GAME000000000000
 */
$response = $client->get(new RequestProductById('UP0001-CUSA09311_00-GAME000000000000'));

4.2. 通过ID请求概念数据

use PlaystationStoreApi\Request\RequestConceptById;

/**
 Example for https://store.playstation.com/en-us/concept/10002694
 */
$response = $client->get(new RequestConceptById('10002694'));

4.3. 请求目录数据

use PlaystationStoreApi\Request\RequestProductList;

$request = RequestProductList::createFromCategory(CategoryEnum::PS5_GAMES);

$firstPageResponse = $client->get($request);

$nextPageResponse = $client->get($request->createNextPageRequest());

5. 运行示例

如果您想运行示例,您需要

  1. Docker和docker compose
  2. 执行make命令,例如
make get_add_ons_by_title_id
  1. response目录获取API响应

关于请求签名

对于所有请求,您需要发送sha256Hash。这是请求签名。

您可以从浏览器请求中获取sha256Hash

  1. 打开网络面板,找到查询https://web.np.playstation.com/api/graphql/v1/op
  2. 复制完整的请求URL并使用urldecode
  3. sha256Hash位于扩展参数中,例如
https://web.np.playstation.com/api/graphql/v1/op?operationName=categoryGridRetrieve&variables={"id":"44d8bb20-653e-431e-8ad0-c0a365f68d2f","pageArgs":{"size":24,"offset":0},"sortBy":{"name":"productReleaseDate","isAscending":false},"filterBy":[],"facetOptions":[]}&extensions={"persistedQuery":{"version":1,"sha256Hash":"9845afc0dbaab4965f6563fffc703f588c8e76792000e8610843b8d3ee9c4c09"}}

如果默认sha256Hash会被阻止,您可以将基本值替换

  1. 获取新的sha256Hash值
  2. PlaystationStoreApi\RequestLocatorService中替换默认值
use PlaystationStoreApi\RequestLocatorService;
use PlaystationStoreApi\Request\RequestPSPlusTier;

$customRequestLocatorService = RequestLocatorService::default();
$customRequestLocatorService->set(RequestPSPlusTier::class, 'new sha256Hash value')
  1. 将$customRequestLocatorService提供给客户端
declare(strict_types=1);

use PlaystationStoreApi\Client;
use GuzzleHttp\Client as HTTPClient;
use PlaystationStoreApi\Enum\CategoryEnum;
use PlaystationStoreApi\Enum\RegionEnum;

const API_URL = 'https://web.np.playstation.com/api/graphql/v1/';

$client = new Client(
    RegionEnum::UNITED_STATES, 
    new HTTPClient(['base_uri' => API_URL, 'timeout' => 5]), 
    $customRequestLocatorService
);

6. 自定义请求

如果您需要自定义请求

  1. 创建新的请求类,然后实现PlaystationStoreApi\Request\BaseRequest
  2. 将新的请求类及其sha256Hash追加到PlaystationStoreApi\RequestLocatorService
  3. 将新的RequestLocatorService提供给客户端
  4. 使用新请求执行客户端的get方法

7. Postman收集

您可以使用postman尝试PlayStation API。

要导入收集,请下载playstation api.postman_collection.json