2

All the examples in php.net regarding url has demostrated only with http and not with https. I really don't understand why this has been not demonstrated with https. The file_get_contents returning true for a http URL and false for a https URL.

  1. Do I need to enable something in the server where this function is being executed?
  2. I am aware that it's a logical error but this logical error is happening in a built in function. I don't have any syntactical error. What am I missing here?
  3. My expected output is to, get a true return from file_get_contents when a https url being passed into it.

Code:

function connectMe() {
   $urlHeader = $_REQUEST['urlHeader'];
   // if this is a http url it's working for file_get_contents returns true
   // if this is a https url it's not working file_get_contents gives false
   $status = send($urlHeader);
   var_dump($status);
   echo $status ? 'success' : 'failed';
   exit;
}

function send($url) {
    $ctx = stream_context_create(
        array(
        'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>NULL
        )
      )
    );
var_dump(stream_get_wrappers());
$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
return file_get_contents($url, 0, $ctx);
}

Note: 1. The above code has been integrated as per the solution given in stackoverflow where nothing changed in the result. 2. The openssl is enabled and allow_url_fopen is on.

OUTPUT that I get:

array(9) { [0]=> string(5) "https" [1]=> string(4) "ftps" [2]=> string(3) "php" [3]=> string(4) "file" [4]=> string(4) "glob" [5]=> string(4) "data" [6]=> string(4) "http" [7]=> string(3) "ftp" [8]=> string(4) "phar" }

openssl: yes http wrapper: yes https wrapper: yes wrappers: array ( 0 => 'https', 1 => 'ftps', 2 => 'php', 3 => 'file', 4 => 'glob', 5 => 'data', 6 => 'http', 7 => 'ftp', 8 => 'phar', )

Warning: file_get_contents(): php_network_getaddresses: gethostbyname failed. errno=0 in FILENAME.PHP on line 40

Warning: file_get_contents(https://someIPhere or website): failed to open stream: php_network_getaddresses: gethostbyname failed. errno=0 in FILENAME.PHP on line 40

bool(false) failed

Common Man
  • 103
  • 2
  • 13
  • 4
    Are you sure `$_REQUEST('urlHeader');` is correct? `$_REQUEST` is not a function – apokryfos Oct 12 '17 at 15:00
  • 2
    Maybe this [link](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) can help – Nebojsa Nebojsa Oct 12 '17 at 15:01
  • 1
    Possible duplicate of [How to get file\_get\_contents() to work with HTTPS?](https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https) – Patrick Q Oct 12 '17 at 15:02
  • Check that you have this in your php config : `extension=php_openssl.dll allow_url_fopen = On` – teeyo Oct 12 '17 at 15:04
  • @PatrickQ - it's not a possible duplicate. Stackoverflow is not working that way. Every problem is unique. You can't fit in my problem for someone else solution. I had tried that link's solution and nothing helped me around. – Common Man Oct 12 '17 at 15:05
  • 1
    @apokryfos I think it should be `$_REQUEST['urlHeader'];` –  Oct 12 '17 at 15:05
  • 3
    @CommonMan Actually, that's _exactly_ how SO works. _Many_ questions posted here are duplicates. And you have given no indication that you have any basic debugging of this problem. Please do enlighten us as to what you have done to try to resolve this and what the results of those attempts were. – Patrick Q Oct 12 '17 at 15:07
  • I have tried with `file()` and it returns the same false. Then `parse_url` which is totally a meant for a different utilization. – Common Man Oct 12 '17 at 15:10
  • thanks for closing but whoever having a lot of points, can anyone fit in the https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https to my problem. Because I have tried and nothing changed. You must be tired of seeing problems similar with very very slight difference. I agree. – Common Man Oct 12 '17 at 15:12
  • @CommonMan If you want people to not assume it's a duplicate then you need to explain **how** you tried the solution proposed in the suggested duplicate and how it didn't work. Can you share the outputs of `stream_get_wrappers()` and `extension_loaded('openssl')` for example? – apokryfos Oct 12 '17 at 15:42
  • @apokryfos - Please check the update and edit in question. – Common Man Oct 12 '17 at 15:50
  • 1
    @CommonMan `gethostbyname` failing means it's a DNS problem. Check if you can ping that host. Also it's a good idea to provide as much information as possible in the question. – apokryfos Oct 12 '17 at 15:52
  • @apokryfos - the IP that I am trying to reach is up. Checked thru: ping in cmd and also in browser. – Common Man Oct 12 '17 at 15:55
  • @PatrickQ - The solution integrated with my problem. There is no change in end result. Is this question still going to be a duplicate!? I agree it's a duplicate as contributors and moderators with high points can mark it. – Common Man Oct 12 '17 at 16:00

1 Answers1

0

Chances are that you might be pointing to a self-signed ssl. If so then that might be the reason. Check to make sure the the URL has a valid SSL Certificate.

If you want to bypass the SSL verification then you will need to add a stream_context_create to set 'verify_peer' => false.

see: file_get_contents(): SSL operation failed with code 1 (certificate verify failed)

ggedde
  • 582
  • 5
  • 12