Jump to content

if file_get_contents fails


dadamssg87

Recommended Posts

i use file_get_contents() to grab entire web pages to send as html emails. How can i error check that the url i use in file_get_contents() actually exists. I thought i could put it in an if statement like cURL but that doesn't work.

 

<?php

if($email_body = file_get_contents($url))
{			
	$config['mailtype'] = 'html';
	$this->email->initialize($config);
	$this->email->from('[email protected]', 'Testing');
	$this->email->to($email);									
	$this->email->subject("Confirm Reply-To Email");
	$this->email->message($email_body);	
	$this->email->send();
        }

Link to comment
https://forums.phpfreaks.com/topic/252139-if-file_get_contents-fails/
Share on other sites

Something like this should work:

 

$curl = curl_init('http://example.com');

curl_setopt($curl, CURLOPT_NOBODY, true);

curl_exec($curl);

$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

if ($http_status == 200) {
    // good to go
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.