php - file_get_contents not working on the server but working fine on local machineGet the solution ↓↓↓
You need to check if your server allows file_get_contents() to open URL. Check if
allow_url_fopen is set to true in your php.ini or server configuration.
You can look at this link to get more help
Information from manual
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
The other option is to use CURL
as the manual says:
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
so fopen wrappers are not enabled on host
i solved it by using this code instead
$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,''); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,200); curl_setopt($ch,CURLOPT_TIMEOUT, 200); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' )); $response = curl_exec($ch);
In my case, for some weird reason my old server setting wont add header with this code
$header = "Authorization: key=AAAAiQXvgZA:APA91bGKc12FiRd3l3jz\r\n"."Content-Type: application/json\r\n"."User-Agent: Mozilla/5.0 (iPad; U; CPU iPad OS 5_0_1 like Mac OS X; en-us) AppleWebKit/535.1+ (KHTML like Gecko) Version/126.96.36.199 Safari/6533.18.5\r\n"; $options = array( 'http' => array( 'method' => 'POST', 'content' => $data, 'header' => $header, "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false ) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context);
I have to walk around by adding the following code before use stream_context_create:
ini_set("user_agent" , "Mozilla/3.0\r\nAccept: */*\r\nX-Padding: Foo\r\nAuthorization: key=AAAAiQXvgZA:APA91bGKc12FiRd3l3jzIgMVsF9Dkn25xF\r\nContent-Type: application/json");
Hope this help someone someday and sorry for my bad english !
Share solution ↓
Link To Answer People are also looking for solutions of the problem: ftp_put(): can't open that file: no such file or directory
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.