3

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.

Community
  • 1
  • 1
Jahid
  • 21,542
  • 10
  • 90
  • 108

4 Answers4

1

Use $http_response_header:

$changelog="https://raw.github.com/neurobin/oraji/release/ChangeLog";
$filec1=@file_get_contents($changelog);

if($filec1===false || $http_response_header[0] == 'HTTP/1.1 404 Not Found') {$filec1="something";}
  • I figured, looking to header response is the way to go. but somehow your method didn't get me the result. Just looking at `200` seems the way to go. copied from http://stackoverflow.com/a/617721/3744681 – Jahid Dec 25 '15 at 13:49
  • 1
    I've uploaded the script to my remote host and github gives me 301 code. So, yes, with such behavior of github it's more likely to just search for 200 in response code. – Denis Alexandrov Dec 25 '15 at 13:56
  • Please look at my edit. My solution has a major flaw. – Jahid Dec 25 '15 at 15:07
  • Seems like it's better to use `CURL` in this situation. Get with it the requested URL and parse curl's result. – Denis Alexandrov Dec 25 '15 at 15:20
  • I have edited and undeleted my answer. It seems to work. Let me know if you find any flaws in it. – Jahid Dec 25 '15 at 15:43
1

Put the file_get_contents in a conditional.

$changelog="https://raw.github.com/neurobin/oraji/release/ChangeLog";
if($filec1 = @file_get_contents($changelog)) {
    echo 'Got it';
} else {
    echo 'NOOOOOoooo!';
}

Also note if you take off the @ you are given an error.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • It doesn't work how, you don't get `Noooo!` or you want some other behavior? – chris85 Dec 25 '15 at 13:42
  • It gets me Not Found. – Jahid Dec 25 '15 at 13:42
  • I've tested on PHP 5.2 locally and it works as expected. Tested 4.3-7 here, and works as expected, https://3v4l.org/bZkHB. Is that the actual URL? – chris85 Dec 25 '15 at 13:44
  • On local host my original code works too. But not in remote... I don't know what is Github doing internally to produce this kind of behavior. – Jahid Dec 25 '15 at 13:45
  • @Switcher You get the `NOOOOOoooo!` which is the intended result/behavior.. Seems OPs remote server is configured strangely. – chris85 Dec 25 '15 at 13:56
  • Yep, that's pretty weird. Github's code logic and the difference in file_get_contents result.... – Denis Alexandrov Dec 25 '15 at 13:58
  • 1
    @chris85, I think I've got it. Github gives redirect(301) to Not Found page(404). My remote doesn't follow the redirect. – Denis Alexandrov Dec 25 '15 at 14:00
  • @Switcher take off the `@` and I think you will get varying `404` warning; https://3v4l.org/Pft2G all still result in the `Noooo!` though. Can't see an instance where it would result in true.. – chris85 Dec 25 '15 at 14:02
0

This is working as expected:

The logic is simple:

Redirect or not, there will be a 200 OK somewhere in $http_response_header array if the URL is valid.

So, I just check over all elements of the array for a 200 OK message.

<?php 
function is200OK($arr){
    foreach($arr as $str){
        if(strpos($str, " 200 OK")) {return true;}
    }
    return false;
}

function fileGetContents($file){
    $filec1=@file_get_contents($file);
    if($filec1===false || !is200OK($http_response_header)) {$filec1="something";}
    //print_r($http_response_header);
    return $filec1;
}

$changelog="https://raw.github.com/neurobin/oraji/release/ChangeLog";
$filec1=fileGetContents($changelog);

echo $filec1;
?>
Jahid
  • 21,542
  • 10
  • 90
  • 108
0

Well,if you access the link you will see you get that error. Links which returns json , look like an array even if they are in web.