fx-1 / api /services /ModrinthService.php
felix-fx's picture
Initial commit from GitHub fx-1
6711fd5 verified
Raw History Blame Contribute Delete
3.47 kB
<?php
// /api/services/ModrinthService.php
class ModrinthService {
private string $apiUrl = 'https://api.modrinth.com/v2';
private string $apiKey;
public function __construct(string $apiKey) {
$this->apiKey = $apiKey;
}
/**
* إنشاء مشروع جديد على Modrinth.
* @param array $projectData بيانات المشروع.
* @return array البيانات المرجعة من Modrinth.
*/
public function createProject(array $projectData): array {
$url = $this->apiUrl . '/project';
// بيانات المشروع يجب أن تتطابق مع مواصفات Modrinth
$data = [
'title' => $projectData['title'],
'description' => $projectData['description'],
'project_type' => $projectData['project_type'],
'requested_status' => 'unlisted', // <--- مهم جداً: unlisted
'initial_versions' => [], // يمكن إضافة النسخة الأولى هنا أو لاحقاً
'categories' => $projectData['categories'] ?? [],
'client_side' => $projectData['client_side'] ?? 'required',
'server_side' => $projectData['server_side'] ?? 'required',
'body' => $projectData['description'], // Modrinth يستخدم body للوصف الطويل
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: ' . $this->apiKey,
'Content-Type: application/json',
'User-Agent: Craft-FX/1.0.0 (contact@craft-fx.com)'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new Exception("فشل إنشاء المشروع على Modrinth: " . $response);
}
return json_decode($response, true);
}
/**
* رفع نسخة جديدة من الملف إلى Modrinth.
*/
public function createVersion(string $projectId, array $versionData, string $filePath): array {
$url = $this->apiUrl . '/version';
$data = [
'data' => json_encode([
'name' => $versionData['name'],
'version_number' => $versionData['version_number'],
'game_versions' => $versionData['game_versions'],
'loaders' => $versionData['loaders'],
'featured' => true,
'project_id' => $projectId,
'file_parts' => ['file']
]),
'file' => new CURLFile($filePath)
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: ' . $this->apiKey,
'User-Agent: Craft-FX/1.0.0 (contact@craft-fx.com)'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new Exception("فشل رفع النسخة إلى Modrinth: " . $response);
}
return json_decode($response, true);
}
}