PHP fopen for URL: Alternatives on Windows - php

I'm using the following code in PHP5 on *nix systems to call a remote URL (handover of 2 parameter to a remote system:
$fp = fopen($url, "r");
$contents = stream_get_contents($fp);
fclose($fp);
Unfortunately it fails for https (http works correctly) on Windows/IIS with the following error message (although allow_url_fopen is definitely enabled):
fopen(https://www.example.com/?method=abc&par=xyz) [function.fopen]: failed to open stream: No error in C:\Inetpub\wwwroot\libs\externalSys.php on line 122
Hence my questions:
Has anyone else found a solution?
Are there any alternatives? Curl
is unfortunately not
installed/available.

This looks like you may not have included the php_openssl.dll in your php.ini, so https fopens will fail. Does it work with just standard http?

You can try using file_get_contents.
Usage:
$contents = file_get_contents($url);

Related

getimagesize / fopen connection refused

I am trying to get the size from an remote image with the native php function getimagesize from an absolute URL.
I am using PHP 7.3, here is the code:
$fichier = 'http://mydomain/images/image.jpg';
$size = getimagesize($fichier);
var_dump($size);
And the code return :
PHP Warning: getimagesize(http://mydomain/images/image.jpg): failed to open stream: Connection refused in ...
And with fopen, same problem:
PHP Warning: fopen(http://mydomain/images/image.jpg): failed to open stream: Connection refused
And when I am trying the same URL from an other server, it is working.
Also, if I am trying to get infos from an other image and from an other domain but on the same server, it is not working also.
allow_url_fopen is enabled.
I think that it should come from Apache conf, but not sure... Anybody has an idea ?
Thanks !
Finally, the problem is coming from the network.
Apache could not resolve his own public domain name.
So to correct the problem, I just had " 127.0.0.1 mydomain" in the hosts file, and the problem was solved.
Hope it can help !

Error when accessing youtube api: 'Warning: file_get_contents() [function.file-get-contents]: URL > file-access is disabled...'

I was trying to follow this answer:
https://stackoverflow.com/a/3331372/1063287
So in my php document I had the variables below:
$json = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_id}?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->{'entry'}->{'title'};
$video_date = $json_data->{'entry'}->{'published'};
$video_duration = $json_data->{'entry'}->{'media:group'}->{'yt$duration'};
$video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$video_description = $json_data->{'entry'}->{'content'};
but that gave me the error:
Warning: file_get_contents() [function.file-get-contents]: URL
file-access is disabled in the server configuration in
/home/path/to//file.php on line 12
Warning:
file_get_contents(https://gdata.youtube.com/feeds/api/videos/xx-xxxxxxx?v=2&alt=json)
[function.file-get-contents]: failed to open stream: no suitable
wrapper could be found in /home/path/to/file.php on line 12
I have looked at the developers guide here:
https://developers.google.com/youtube/2.0/developers_guide_php
and can understand the feed and entry structure here:
https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries
and have found a few solutions (one involving curl) but they either haven't worked or I haven't known if they were the best method to use.
Can anyone please tell me if there is anything wrong in the above code?
This is most likely because you do not have OpenSSL installed on your server. Try installing it, and if it still doesn't work it's probably because you have HTTP access disabled in your php.ini (set allow_url_fopen to true)
The best thing I saw for that is the Zend_GData_Youtube in the Zend Framework. It doesn't require anything special but it's much easier than a normal cURL or JSON request!
See there: http://framework.zend.com/downloads/latest

PHP fopen() failed in combination session_start() and mysql_connect()

We have a problem on Debian server and PHP version 4.4.4-8+etch6. In this script (it is full script, not just part of code):
mysql_connect('localhost', 'user', 'pass');
session_start();
$h = fopen('http://www.google.com/robots.txt', 'r');
var_dump(fread($h, 40));
If I comment out mysql_connect, it works. If i comment out session_start, it works.
If mysql_connect failed (bad user and password) fopen works too. But with both, fopen failed. So it is not related to DNS lookup.
There is a result
Warning: fopen(http://www.google.com/robots.txt) [function.fopen]: failed to open stream: HTTP request failed! in /var/www/web4/rychtarka.cz/rychtarka.cz/test.php on line 9
Warning: fread(): supplied argument is not a valid stream resource in /var/www/web4/rychtarka.cz/rychtarka.cz/test.php on line 10
bool(false)
I think, there is some resources limit in system, file descriptors or something. Our server administrator told us, it is skript error. Can you please point me to debian system change to repair.
Thank you for any advice.
Finally (after 3 days) we fixed the error. We traced it using 'php -f test.php' which pointed to Apache's limit on max open files.
We raised number via ulimit from 1024 to 8k and after a restart everything worked great.

file_get_contents() error

I am using file_get_contents on my PHP and it throws some errors:
My code
#try to fetch from remote
$this->remotePath = "http://some-hostname.com/blah/blah.xml
$fileIn = #file_get_contents($this->remotePath);
The errors:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /virtual/path/to/file/outputFile.php on line 127
Warning: file_get_contents(https://some-host-name/data/inputFile.xml) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /virtual/path/to/file/outputFile.php on line 127
Any idea? It worked fine in my computer but stopped working when I ported it to the web server.
Your server must have the allow_url_fopen property set to true. Being on a free webhost explains it, as it's usually disabled to prevent abuse. If you paid for your hosting, get in contact with your host so they can enable it for you.
If changing that setting is not an option, then have a look at the cURL library.
It seems "allow_url_fopen" setting is false on your server and hence does not allow using URLs with file_get_contents().
Try using CURL instead that is a better and efficient way of communicating with other server.

Warning on PHP fopen

$file = #fopen("http://ajax.htm","rb");
I am getting this error while using fopen
Warning: fopen(xyz.htm): failed to open stream: HTTP request failed!
in /home/user/public_html/aaa/ttt.php on line 8
what could be the reason behind this?
I am going to go out on a huge limb here and suggest the problem is the url, 'http://ajax.htm'.
from docs:
If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
edit: ajax.htm is not a valid url.
$file = #fopen("ajax.htm","rb");
there is your problem i think
http://nl3.php.net/function.fopen

Categories