user9182530
user9182530

Reputation:

How do I do an "if domain does not exist"?

So, I have a Laravel app to convert pages from url to pdf. I'm using file_get_contents to get the page on a string to convert it to pdf. I'm having some problems with the input,I'm using "filter_var" to check valid url, and check if it starts with http, but I have errors when its a valid domain but it does not load or when it takes to long.

if ((substr( $url, 0, 8 ) === 'https://') || (substr( $url, 0, 7 ) === 'http://'))
{
    if (filter_var($url, FILTER_VALIDATE_URL))
    {
        $content = file_get_contents($url);
        $parse = parse_url($url);
    }
    else
    {
        session()->flash('danger', 'Imposible to load the url');
        return view('welcome');
    }
}
else
{
    session()->flash('danger', 'You must set a proper url type value');
    return view('welcome');
}

How can I check if is taking to long to load or if is a working domain? Or any other idea? Any tip is wellcome, thanks.

Upvotes: 1

Views: 162

Answers (1)

user9182530
user9182530

Reputation:

I have been advised to use curl() insted of file_get_contents() because curl allows to handle the request and errors much better. So I have found a nice function here (How to get page content using cURL?).

protected function get_web_page( $url )
{
    /**
    * Send a GET requst using cURL
    * @param string $url to request
    * @param array $user_agent values to send
    * @param array $options for cURL
    * @return string
    */
    $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

    $options = array(

        CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
        CURLOPT_POST           =>false,        //set to GET
        CURLOPT_USERAGENT      => $user_agent, //set user agent
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

And I have use it like this

if ((substr( $url, 0, 8 ) === 'https://') || (substr( $url, 0, 7 ) === 'http://'))
{
    $result = $this->get_web_page( $url );
    if ( $result['errno'] != 0 )
    {
        session()->flash('danger', 'The url is not returning anything or is taking too long.');
        return view('welcome');
    }
    if ( $result['http_code'] != 200 )
    {
        session()->flash('danger', 'There is no page to display.');
        return view('welcome');
    }
    $content = $result['content'];
}
else
{
    session()->flash('danger', 'You must set a proper url type value');
    return view('welcome');
}

Upvotes: 2

Related Questions