Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents( ) not working

Tags:

php

curl

apache

I have a script that uses file_get_contents() to get json response from remote server . while file_get_contents() is working properly on local files but not working with http or https it gives me the following error file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in and file_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found .. it is a dedicated server and I have WHM .. I've tried

  1. setting allow_url_fopen = on on WHM PHP configuration Editor but This didn't work.
  2. creating a php.ini file with allow_url_fopen = on in the directory where the error occurred but this didn't work.
  3. added ini_set('allow_url_fopen', 'On'); to the start of the PHP script but this didn't work .

I know I can use Curl instead but I want to know why this is not working .. the script is working properly on other server and localhost

update :

phpinfo();

gives me

allow_url_fopen Off 
allow_url_include   Off 
always_populate_raw_post_data   Off

that means my php.ini changes didn't take effect .. what I'm doing wrong ?

like image 249
Kamal Saleh Avatar asked Dec 23 '14 02:12

Kamal Saleh


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What will the file_get_contents () return?

The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads a file into a string. The file_put_contents() function writes data to a file.

Does file_get_contents cache?

Short answer: No. file_get_contents is basically just a shortcut for fopen, fread, fclose etc - so I imagine opening a file pointer and freading it isn't cached.


1 Answers

$url= 'https://example.com';

$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

This will allow you to get the content from the url whether it is a HTTPS, no need to change in php ini.

like image 74
Dushyant Dagar Avatar answered Oct 24 '22 17:10

Dushyant Dagar