1
votes

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

1
Are you sure $_REQUEST('urlHeader'); is correct? $_REQUEST is not a functionapokryfos
Maybe this link can helpNebojsa Nebojsa
@apokryfos I think it should be $_REQUEST['urlHeader'];user8757663
@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

1 Answers

0
votes

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)