74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Mail\SimpleMail;
|
|
use App\Utilities\Notice;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class CloudflareSolverService
|
|
{
|
|
protected string $solverUrl = 'http://flaresolverr:8191/v1';
|
|
|
|
/**
|
|
* Holt die Daten und gibt das extrahierte JSON als PHP-Array zurück.
|
|
* Wenn etwas schiefgeht, wird null zurückgegeben.
|
|
*/
|
|
public function getJsonFromUrl(string $targetUrl): ?array
|
|
{
|
|
|
|
$this->solverUrl = config('services.flaresolverr.id_docker');
|
|
|
|
try {
|
|
$response = Http::withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
])->timeout(120)->post($this->solverUrl, [
|
|
'cmd' => 'request.get',
|
|
'url' => $targetUrl,
|
|
'maxTimeout' => 90000,
|
|
]);
|
|
|
|
if ($response->failed()) {
|
|
// Log::error('FlareSolverr Verbindungsfehler. Status: ' . $response->status());
|
|
|
|
return null;
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
if (strpos($data['solution']['response'], '"code": 403, "reason": "challenge"') !== false) {
|
|
Mail::to('hermann.telser@gmail.com')->send(new SimpleMail('STATISTIKO', 'Error 403 challenge'));
|
|
dd('challenge');
|
|
}
|
|
|
|
if (isset($data['status']) && $data['status'] === 'ok') {
|
|
$htmlContent = $data['solution']['response'];
|
|
|
|
// JSON aus dem HTML isolieren
|
|
if (preg_match('/\{.*\}/s', $htmlContent, $matches)) {
|
|
$rawJson = $matches[0];
|
|
$decoded = json_decode($rawJson, true);
|
|
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
// HIER: Erfolgreiche Rückgabe des Arrays!
|
|
return $decoded;
|
|
}
|
|
|
|
// Log::error('Extrahiertes Textfragment ist kein gültiges JSON.');
|
|
} else {
|
|
// Log::error('Kein JSON-Muster { ... } im HTML gefunden.');
|
|
}
|
|
} else {
|
|
// Log::error('FlareSolverr konnte die Seite nicht umgehen: '.json_encode($data));
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Log::error('PHP Exception im CloudflareSolverService: '.$e->getMessage());
|
|
}
|
|
|
|
Notice::note('ERROR connecting FLARESOLVERR_IP_DOCKER', 'br', 1);
|
|
|
|
return [];
|
|
}
|
|
}
|