Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

49 wiersze
1.5KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use Core\Controller;
  5. use Core\Request;
  6. use Core\Response;
  7. class ApiProxyController extends Controller
  8. {
  9. public function fetch(): Response
  10. {
  11. $request = Request::capture();
  12. $url = trim((string) ($request->input('url') ?? ''));
  13. if ($url === '' ||
  14. !filter_var($url, FILTER_VALIDATE_URL) ||
  15. !in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)
  16. ) {
  17. return Response::json(['error' => 'Invalid or missing URL.'], 400);
  18. }
  19. $ch = curl_init();
  20. curl_setopt_array($ch, [
  21. CURLOPT_URL => $url,
  22. CURLOPT_RETURNTRANSFER => true,
  23. CURLOPT_TIMEOUT => 10,
  24. CURLOPT_FOLLOWLOCATION => true,
  25. CURLOPT_MAXREDIRS => 3,
  26. CURLOPT_SSL_VERIFYPEER => true,
  27. CURLOPT_USERAGENT => 'CampaignTracker-ApiProxy/1.0',
  28. CURLOPT_HTTPHEADER => ['Accept: application/json, application/xml, text/xml, text/plain, */*'],
  29. ]);
  30. $body = curl_exec($ch);
  31. $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  32. $curlErr = curl_error($ch);
  33. curl_close($ch);
  34. if ($body === false) {
  35. return Response::json(['error' => $curlErr ?: 'Outbound request failed.'], 502);
  36. }
  37. return Response::json(['body' => (string) $body, 'http_status' => $httpCode]);
  38. }
  39. }

Powered by TurnKey Linux.