Catch When cURL can't connect to webserver
If a domain is invalid or a web server is down, cURL will return false and not have a status code. We need to catch it and distinguish the difference between page not found and cannot connect for the user.
This commit is contained in:
parent
6c1aaeae2a
commit
03b72c9198
@ -20,7 +20,8 @@
|
|||||||
define(DEBUG,false);
|
define(DEBUG,false);
|
||||||
|
|
||||||
define(MAXTIMEOUT,30);
|
define(MAXTIMEOUT,30);
|
||||||
define(ONFAILIMAGE,'https://http2pic.haschek.at/img/failed.jpg');
|
define(ONFAILIMAGE, __DIR__.'/img/pagefailed.jpg');
|
||||||
|
define(ONDOMAINFAILIMAGE, __DIR__.'/img/domainfailed.jpg');
|
||||||
|
|
||||||
//rendering engine: wkhtmltoimage or phantomjs
|
//rendering engine: wkhtmltoimage or phantomjs
|
||||||
define(RENDERINGENGINE,'wkhtmltoimage');
|
define(RENDERINGENGINE,'wkhtmltoimage');
|
||||||
@ -94,6 +95,11 @@ class http2pic
|
|||||||
$this->params['onfail'] = ONFAILIMAGE;
|
$this->params['onfail'] = ONFAILIMAGE;
|
||||||
else
|
else
|
||||||
$this->params['onfail'] = rawurldecode($this->params['onfail']);
|
$this->params['onfail'] = rawurldecode($this->params['onfail']);
|
||||||
|
|
||||||
|
if(!$this->params['ondomainfail'])
|
||||||
|
$this->params['ondomainfail'] = ONDOMAINFAILIMAGE;
|
||||||
|
else
|
||||||
|
$this->params['ondomainfail'] = rawurldecode($this->params['ondomainfail']);
|
||||||
|
|
||||||
|
|
||||||
//validate URL and check if exists
|
//validate URL and check if exists
|
||||||
@ -103,10 +109,20 @@ class http2pic
|
|||||||
$this->params['url'] = rawurldecode($_GET['url']);
|
$this->params['url'] = rawurldecode($_GET['url']);
|
||||||
|
|
||||||
//if the url is not valid or not responding, show onfail image and leave
|
//if the url is not valid or not responding, show onfail image and leave
|
||||||
if(!$this->isURLValid($this->params['url']) || !$this->isURLReachable($this->params['url']))
|
if(!$this->isURLValid($this->params['url']) || !(($reachableResult = $this->isURLReachable($this->params['url'])) == 0))
|
||||||
{
|
{
|
||||||
header('Content-Type: image/jpeg');
|
header('Content-Type: image/jpeg');
|
||||||
$result = imagecreatefromjpeg($this->params['onfail']);
|
header('Content-Disposition: inline; filename="http2png.jpg"');
|
||||||
|
switch ($reachableResult) {
|
||||||
|
case 1:
|
||||||
|
header('HTTP/1.0 404 File Not Found');
|
||||||
|
$result = imagecreatefromjpeg($this->params['onfail']);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
header('HTTP/1.0 404 Server Not Found');
|
||||||
|
$result = imagecreatefromjpeg($this->params['ondomainfail']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
imagejpeg($result, NULL, 100);
|
imagejpeg($result, NULL, 100);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -283,16 +299,22 @@ class http2pic
|
|||||||
{
|
{
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_exec($ch);
|
if(curl_exec($ch) != false){
|
||||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
//We were able to connect to a webserver, what did it return?
|
||||||
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
if($code < 400) //status code updated so redirects will also work
|
|
||||||
$status = true;
|
if($code < 400) //status code updated so redirects will also work
|
||||||
else
|
$status = 0;
|
||||||
$status = false;
|
else
|
||||||
|
$status = 1;
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
return $status;
|
return $status;
|
||||||
|
} else {
|
||||||
|
//We were not able to connect to any webserver so we didn't get a status code
|
||||||
|
//to compare against. There must be a problem with the domain that was supplied.
|
||||||
|
curl_close($ch);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function trimToAlphaNumeric($string)
|
function trimToAlphaNumeric($string)
|
||||||
|
BIN
img/domainfailed.jpg
Normal file
BIN
img/domainfailed.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
BIN
img/pagefailed.jpg
Normal file
BIN
img/pagefailed.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
@ -124,9 +124,14 @@ define('CONN', (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "")?'http':'ht
|
|||||||
<td>onfail</td>
|
<td>onfail</td>
|
||||||
<td>[url of .jpg]</td>
|
<td>[url of .jpg]</td>
|
||||||
<td>If the page can't be reached, this image will be displayed instead</td>
|
<td>If the page can't be reached, this image will be displayed instead</td>
|
||||||
<td><?php echo CONN; ?>://<?php echo DOMAIN; ?>/img/failed.jpg</td>
|
<td><?php echo CONN; ?>://<?php echo DOMAIN; ?>/img/pagefailed.jpg</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>ondomainfail</td>
|
||||||
|
<td>[url of .jpg]</td>
|
||||||
|
<td>If the web server can't be reached, this image will be displayed instead</td>
|
||||||
|
<td><?php echo CONN; ?>://<?php echo DOMAIN; ?>/img/domainfailed.jpg</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>cache</td>
|
<td>cache</td>
|
||||||
<td>[any alphanumeric string]</td>
|
<td>[any alphanumeric string]</td>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user