This is the code I am using:
<?php
$changelog="https://raw.github.com/neurobin/oraji/release/ChangeLog";
$filec1=@file_get_contents($changelog);
if($filec1===false) {$filec1="something";}
echo $filec1
?>
It prints Not Found instead of something. But when I add another condition to the if statement like this:
if($filec1===false||$filec1=="Not Found") {$filec1="something";}
Then it works as expected.
What's going wrong here?
PHP version is 5.4. php -v
output:
PHP 5.4.45 (cli) (built: Oct 29 2015 09:16:10)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with the ionCube PHP Loader v4.7.5, Copyright (c) 2002-2014, by ionCube Ltd., and
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies
with Suhosin v0.9.36, Copyright (c) 2007-2014, by SektionEins GmbH
N.B: I am doing this on a remote server.
EDIT:
Anyway, I noticed (going to that URL in browser) that Github is sending literal 'Not Found' as a content for that non-existing URL (I don't know why). But how can I workaround it (without using a literal string as conditional)?
This is what I ended up doing:
As per this answer, I am checking for the HTTP header response and targeting 200
as a success code and failure otherwise (along with a true/false check).
<?php
function fileGetContents($file){
$filec1=@file_get_contents($file);
if($filec1===false || !strpos($http_response_header[0], "200"))
{$filec1="something";}
return $filec1;
}
$changelog="https://raw.github.com/neurobin/oraji/release/ChangeLog";
$filec1=fileGetContents($changelog);
echo $filec1;
?>
Note:
If a 301/302 redirect is used then this won't work. For example, if the above link did even exist, it wouldn't work i.e it would return 'something' instead of the actual content in the redirected page. Because raw.github.com
is redirected to raw.githubusercontent.com
This solution only works if I use the actual URL with no redirection. So this is still not a good solution to go for.