It works perfectly on localhost with Xampp, but when I try it on my server (host-ed.net), fopen can\'t open any url, it only works for files without http.
&l
As http://php.net/manual/en/function.fopen.php states,
If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.
I would therefore assume that there is no registered wrapped for url's, and php is treating that as a regular filename, which doesn't exist.
fopen can't open any url
Check the value of the allow_url_fopen php.ini setting:
var_dump(ini_get('allow_url_fopen'));
It's probably false. You'll need to speak to your web host, or try another method. Mabye CURL is enabled?
You should also check your error_reporting and display_errors values. PHP should have complained loudly about being unable to open the URL. You'll want to turn both of them all the way up while developing code, so you can understand what goes wrong easier.
Search for allow-url-fopen
in your PHP.ini.
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Do you have a free hosting account? Most hosting services limit outgoing connection for free accounts to prevent abuse. Try this:
$fp = fsockopen("some.alive.site", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo "Everything is OK";
fclose($fp);
}
If you'll see an error message - you are not allowed to make outgoing connections.
If you are using PHP-FPM, also check the configuration files in pool.d/
. PHP-FPM pools can override php settings by using php_admin_flag
, php_admin_value
, php_flag
or php_value
. (We encountered this on a Ubuntu 16 vps)
The permission for opening urls with fopen
is controlled by the php.ini setting allow_url_fopen. You can see your current setting using phpinfo() which will dump an html doc containing all your server settings.