Laravel Türkiye Discord Kanalı Forumda kod paylaşılırken dikkat edilmesi gerekenler!Birlikte proje geliştirmek ister misiniz?

Kullanıcıların ekledikleri url lerin gerçekten çalışıp çalışmadığını kontrol etmek istiyorum. Bunun için mevcut validationa yeni bir kural ekledim.
Kod iyi çalışıyor 400 ve 500 hatalarını yakalıyor ve hata donuyor. Fakat sayfa hiç yoksa cURL error 6: Could not resolve host: hatasını donuyor.
Budurumu nasil cözebilirim.

''' <?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Http;

class Urlcheck implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$response = Http::get($value);

    if ($response->clientError()) {
        $fail('The :attribute client.');
    }
    if ($response->serverError()) {
        $fail('The :attribute server.');
    }
}

} '''

  • mgsmus bunu yanıtladı.
    • En İyi Yanıtaeneas tarafından

    aeneas

    try {
        $response = Http::timeout(5)
            ->get($value)
            ->throw();
    
        return true;
    
    } catch(Throwable $exception) {
        return false;
    }
    • mgsmus

      Seviye 1384
    • Düzenlendi
    • En İyi Yanıtaeneas tarafından

    aeneas

    try {
        $response = Http::timeout(5)
            ->get($value)
            ->throw();
    
        return true;
    
    } catch(Throwable $exception) {
        return false;
    }

      mgsmus Çok teşekkürler hocam.

      tam olarak aşağıdaki gibi cözdüm.

      try {
      $response = Http::timeout(2)
      ->get($value)
      ->throw();

                  if ($response->clientError()) {
                      $fail('The :attribute client.');
                  }
                  if ($response->serverError()) {
                      $fail('The :attribute server.');
                  }
      
                  libxml_use_internal_errors(true);
                  if (!simplexml_load_string($response->body())) {
                      $fail(' xml değil.');
                      }
      
      
              } catch(\Illuminate\Http\Client\ConnectionException $e) {
              $fail('böle bir sayfa yok');
          }