How to use php fopen() with a non-standard port? - php

I need my website to be able to use fopen() to access files on my file streaming server, which our admin has configured to use port 8000. But it errors out and I believe it's because of the port number. I have allow_url_fopen enabled in php.ini too, so I don't think that's the issue.
Warning: fopen(http://129.237.213.244:8000/mp3/First_8864.mp3) [function.fopen]:
failed to open stream: Connection refused in _npp_get_mp3() (line 23 of
/home2/ab83417/public_html/drupal/sites/all/modules/npr_player_pack/npp_library.php).
So my question is: Can I somehow tell the webserver or our stream server (icecast, btw) to stop being so picky, or should I use some other php function like curl?

You should make sure that you have allow_url_fopen set to 1.
Launch the following script:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
echo ini_get('allow_url_fopen')."<br />";
$x = file_get_contents('http://129.237.213.244:8000/mp3/First_8864.mp3');
$y = fopen('http://129.237.213.244:8000/mp3/First_8864.mp3',"rb");
It gives me simple 1 and no warnings. If you also get 1 but have warnings you should run the script in other browser and change your ip earlier (for example vpn). It's possible that this site has banned your ip so you cannot connect with this site/url.

Related

File functions not working as expected with AWS PHP Streamwrapper

I have this PHP code running in two different environments: an on-prem server running PHP 5.6 and an EC2 instance running PHP 7.2.
$filepath = 's3://'.$this->s3Bucket.'/'.$key;
$this->s3Client->registerStreamWrapper();
echo "file exists ".file_exists($filepath);
The file functions (e.g., file_exists and readfile) don't appear to work the same way in both environments. In the on-prem server, they work as expected: that is, file_exists returns true for known file, as expected. But on the EC2 instance, file_exists returns false.
The $filepath var looks like s3://my-bucket/a-prefix/another-prefix/LlPIpMvDdmX6i38.pdf. The S3 client and stream wrapper all appear to be correct with var_dump. The only thing I can find is that the PHP file functions simply don't work as expected.
I also tried this line in the EC2 instance:
filesize($filepath);
And go this warning: filesize(): stat failed for s3://...LlPIpMvDdmX6i38.pdf
Is there something I missing in the EC2 set-up that is preventing Streamwrapper from working?
I finally found the error in the error logs with the help of a colleague.
PHP Warning: readfile(): s3:// wrapper is disabled in the server configuration by allow_url_fopen=0
Setting allow_url_fopen=1 fixed the problem.

PHP: Warning: file_get_contents

I am no coder at all. Started learning html and php and bumped into a problem. There a few similar questions, but with my skill, I am not able to implement solutions.
$url = "https://www.bitstamp.net/api/ticker/";
$fgc = file_get_contents($url);
$json = json_decode($fgc, true);
$price = $json["last"];
$high = $json["high"];
$low = $json["low"];
$date = date("m-d-Y - h:i:sa");
$open = $json["open"];
After launching XAMPP on my machine and uploading php file, I get returned:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/lampp/htdocs/crypto_ticker/api.php on line 5
Warning: file_get_contents(https://www.bitstamp.net/api/ticker/): failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/lampp/htdocs/crypto_ticker/api.php on line 5
Would someone please let me know what does that mean? Inability to connect to the page? The page loads itself perfectly fine, so must be something missing on my side.
And what do I do then?
Thank you in advance
Based on the error (Temporary failure in name resolution), it appears to be an issue with the PHP server accessing the website (bitstamp.net). While your web browser might be able to access it, the PHP service on the XAMPP server may not be able to communicate properly with it.
Try running nslookup www.bitstamp.net to ensure that a response is, in fact, provided. If it does resolve, try ensuring your connection is not incorrectly passing through a proxy and ensure your DNS settings are correct.
Based on other responses (https://stackoverflow.com/a/6361464/9549215), ensuring your ServerName directive in your Apache2 Virtual Host is correctly set MAY solve the issue; I have not tested this.
Best of luck with your problem!
You can see the exact error in your output.
Temporary failure in name resolution
so you can't resolve the hostname "www.bitstamp.net". So you should check if you server has the correct DNS settings and can resolve the domain. You can use nslookup or dig to check it.

PHP - fopen($url) failed to open stream: Permission denied in

I have to website with a very similar php configuration (actually on the same hosting account), with almost identical php code and with identical file structure. At some point, I call fopen(http://example.com/rssfedd.xml) to retrieve a RSS feed.
on http://sfdmorin.com/accueil/index.php everything works fine, on http://danielpoiriergda.ca/accueil/index.php i get:
Warning: fopen(http://affaires.lapresse.ca/rss/2399.xml)
[function.fopen]: failed to open stream: Permission denied in
/var/www/vhosts/danielpoiriergda.ca/httpdocs/snippet/lastRSS.php on
line 143
$f = fopen("http://affaires.lapresse.ca/rss/2399.xml", 'r');
Both website have php debug mode enabled and both have the same permissions for file and folders.
Both info.php have *allow_url_fopen* enabled for local and master value
Here are the major differences I found on both phpinfo() file:
not working site: Server API CGI/FastCGI
working site: Server API Apache 2.0 Handler
and on the working site there are no php.ini file in the path given by the loaded configuration file field
EDIT:
I know this thread is probably dead, but not the problem.
I check, with Netbeans fill diff, all the PHP code and php.ini are exactly the same on both domain.
I know similar questions were ask, but they did not help me.
Thank you!
Finally, I contacted my webserver tech support and they solve the problem. They did by:
adding an exception to SELinux
I dont have a clue wwhat SELinux is or what they did specificaly.

PHP php_network_getaddresses: getaddrinfo failed: No such host is known

I am having DNS issues with a certain target domain. I am using fopen() (but same issue with other functions) to retreive an image, but I get this error:
Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known
I am able to ping or nslookup the domain from the command prompt, but for some reason php throws this error. When I try fopen('http://www.google.com', r); or other domains, all goes fine. But above mentioned domain simply won't resolve with PHP. I have flushed the DNS, restarted Apache, but still no luck..
I have tried with:
Windows 7, Apache 2.2 PHP 5.3.6
Windows server 2008r2, Apache 2.2 PHP 5.3.6
What can cause this single domain to not resolve?
IMO it's the different way to resolve a name from the OS and PHP.
Try:
echo gethostbyname("host.name.tld");
and
var_export (dns_get_record ( "host.name.tld") );
or
$dns=array("8.8.8.8","8.8.4.4");
var_export (dns_get_record ( "host.name.tld" , DNS_ALL , $dns ));
You should found some DNS/resolver error.
Your "localhost" cannot resolve the name www.google.com, which means your machine doesn't/can't reach a valid dns server.
Try ping google.com on the console of that machine to verify this.
It is more flexible to use curl instead of fopen and file_get_content for opening a webpage.
getaddrinfo failed: No such host is known
The above error is caused due to mistake in Database Host (DB_HOST) in .env file
Open the .env file and Specially check for this env variables
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password
Your target domain might refuse to send you information. This can work as a filter based on browser agent or any other header information. This is a defense against bots, crawlers or any unwanted applications.
I had a lot of issues with this on Windows. I just performed git clone, copied my .env.example file to .env. updated it, and executed my trusty PHP script to update initial database used for a project. And it failed, and whatever I did, it kept on failing. Out of desperation, I reinstalled MySQL, but that didn't fix things.
Finally, I figured it out. PHP script extracted the settings from .env file using file_get_contents(), and exploding it using \n, but the .env file was encoded with CRLF, so my DB_HOST was empty. After I changed the encoding of the .env file, everything was fine.
So, my point is, echo the parameters supplied to mysqli::construct, and it might be the culprit.
A weird thing I found was that the environment variable SYSTEMROOT must be set otherwise getaddrinfo() will fail on Windows 10.
What had caused this error on my side was the following line
include_once dirname(__FILE__) . './Config.php';
I managed to realize it was the culprit when i added the lines:
//error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
//ini_set('display_errors', 1);
to all my php files.
To solve the path issue i canged the offending line to:
include_once dirname(__FILE__) . '/Config.php';

MagpieRSS: Failed to fetch error

When my script parses an RSS feed from my local Apache server, it parses fine but when I upload script to the remote hosting server it gives the error:
Warning: Magpie RSS: Failed to fetch (url) and cache is off in magpierss-0.6/rss_fetch.inc
on line 231.
I have searched for possible answers and any suggestions are around enabling and changing cache lifetime. I think that is not the problem - it looks like the problem is with remote hosting Apache server or denied access to my host.
Can anyone help?
check that magpie can open the rss url. Usually with curl being enabled or allow_url_fopen on your server. Also make sure that magpie is able/allowed/permitted to create caching files (if any)
This could be caused by a bad url, the server is busy or not having remote file access allowed in your host.
$errormsg = "Failed to fetch $url "; in source indicative of problem getting to host.
Try checking access from the remote host - if you have ssh & curl enabled just do
curl (url)

Categories