file_get_contents failing - php

I have a blog on tumblr, and I'm trying to access it via php and tumblrs api.
the api endpoint is:
http://blog.example.com/api/read/json?start=0&num=10&debug=1
and that seems to work fine.
However, when I do:
$result = file_get_contents("http://blog.example.com/api/read/json?start=0&num=10&debug=1");
print_r($result);
the browser outputs
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/to/file/test.php on line 5
Warning: file_get_contents(http://blog.example.com/api/read/json?start=0&num=10&debug=1) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/to/file/test.php on line 5
This also fails with Curl.
Ideas?
I think this has something to do with the fact that my blog is on a subdomain. The reason I say this, is, if I run the same script on a blog located on the root url, it works fine. Is there a correct way to escape subdomains?

This code is failing because the DNS lookup is failing. This could be because your server is having trouble contacting its name servers. Or its because blog.example.com doesn't exist (it fails in my browser so it should fail everywhere). You could try changing your name servers to google's: 8.8.8.8 and 8.8.4.4. These DNS servers work well.

New Tumblr API: http://www.tumblr.com/docs/en/api/v2
If you have your domain with your tumblr account it will work. An example

Related

simplexml_load_file just stopped working

I read a blog into my website using somehting like this...
define('BLOG_PATH', 'http://kvfcblog.blogspot.co.uk/feeds/posts/default');
$url = BLOG_PATH . "?alt=rss&category=" . $page;
$xml = simplexml_load_file($url);
where $page = index, so reading from
http://kvfcblog.blogspot.co.uk/feeds/posts/default?alt=rss&category=index
this works fine on my localhost, and used to work just as well on my live site, until today, when I started getting these errors ...
simplexml_load_file(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/shdpbmib/public_html/rss/kvfc_rss.php on line 17
simplexml_load_file(http://kvfcblog.blogspot.co.uk/feeds/posts/default?alt=rss&category=index): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/shdpbmib/public_html/rss/kvfc_rss.php on line 17
simplexml_load_file(): I/O warning : failed to load external entity "http://kvfcblog.blogspot.co.uk/feeds/posts/default?alt=rss&category=index" in /home/shdpbmib/public_html/rss/kvfc_rss.php on line 17
Strangley, it still works fine from my localhost
Any ideas? I tried rawurlencode(), but that didn't make any difference
I see that this is getting pretty old but... can you ping the address you're trying to reach? Or can you reach it any other way?
ping your_server_ip
ping stackoverflow.com
If pinging fails, that means something's wrong with your server. It looks like the hostname you're passing to run can't be resolved. You can try:
import socket
socket.getaddrinfo('localhost', 8080)

PHP error doing file_get_contents on a Hostinger host: failed to open stream

I'm trying to use the http://ip-api.com/json/ api for get and show the information of a visitant that conects to my website on hostinguer.com.
When I do the next, it should responds me with a json with all the info in a json format:
echo file_get_contents("http://ip-api.com/json/{$user_ip}");
But I get the following error message:
Warning: file_get_contents(http://ip-api.com/json/[MY PUBLIC IP]): failed to open stream: Connection refused in /home/[HOSTINGUER USER]/public_html/ip2.php on line 31
And other strange thing: if I use another API it works correctly and returns me the correct Json! The other API command:
echo file_get_contents("http://ipinfo.io/{$user_ip}/json");
So, I want to use the ip-api.com API cause the results are more accurate, but only works if I use ipinfo.io API... Why can I do the petition to a website and not to the other?
Otherwhise, I tried both in local using curl or writting in my webrowser and it works correctly. Also I tried in local in a Lammp and the both works perfectly. And finally I tried somthing like this post: PHP file_get_contents() returns "failed to open stream: HTTP request failed!" in hostinguer and it doesn't works..
I thought maybe is something in the hostinguer configuration but...
Thanks in advance!
Your IP address is banned. If you're using normal web hosting, your outgoing IP address is shared with other users, who probably did more requests than allowed.
Go to http://ip-api.com/docs/unban and enter your server's outgoing IP address (check it via http://ipinfo.io/json)

Magento API access on localhost without using internet connection

I have installation of magento 1.6.1 on local machine i\’m trying to connect magetno\’s api in php using following code
$proxy = new SoapClient('http://localhost/magento/api/v2_soap/?wsdl');
$sessionId = $proxy->login('apiuser', 'apikey');
I can connect using this code if internet connection on, If I make it off it doesn't work.
it gives following errors
Warning: SoapClient::SoapClient() [soapclient.soapclient]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\wamp\www\magentomanager\api\products.php on line 12
Warning: SoapClient::SoapClient(http://schemas.xmlsoap.org/soap/encoding/) [soapclient.soapclient]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\wamp\www\magentomanager\api\products.php on line 12
please let me know what is the reason behind that?.
why magento needed internet connection for accessing local api. how do I access api without having without internet connection?
Magento's API is based on SOAP, as you may have found out. SOAP is based on XML and uses therefor schema files, which desribes the schema/layout for the SOAP calls for this service. So this file might be loaded before/on every SOAP call.
However, a workaorund could be to store the schema description on your local machine and editing Magento's wsdl.xml files, so that they will reference to your local machine. Simply search for the URL in all xml files inside your Magento installation.
Not a very nice workaround, but hey,... it's a workaround.
I think the problem is that localhost can't be resolved to your local IP or 127.0.0.1. You can add it in your host file or call it thru http:/// instead of http://localhost

PHP file_get_contents() does not work after uploading to the webserver

Sample code:
$html = file_get_contents('http://www.google.com');
echo $html;
It works on localhost. But after uploading to the webserver, it gives me a warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution. If I replace the domain name with google's IP address, it gives a warning: failed to open stream: Connection timed out.
And ini_get("allow_url_fopen") return 1.
===
Edit: Probably it is a server problem. I guess this question should be asked on serverfault.com.
Your host is most likely refusing the outbound connection. It's the only thing I can think of.
This is a known bug in some versions of PHP. Tell your host about the problem and ask them to upgrade PHP.
It's also possible that your host have extremely naive firewall rules in place blocking just about any networking you try to do. Which host is it?

file_get_contents(): No such host is known error

I am using the file_get_contents function to get the URL into a string:
$contents = file_get_contents("http://google.com");
But, I am getting the following error:
file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I solve this in my localhost?
Your "localhost" cannot resolve the name google.com to an ip address, which means your machine doesn't/can't reach a valid dns server.
This probably doesn't only affect php. Try ping google.com on the console of that machine. Does this complain about the unknown host, too?
The steps necessary to solve the problem depend on what your "localhost" is (operating system, flavour/distribution, ...) and how it is connected to the internet.
It is because you need to turn on allow_url_fopen in php.ini
Check your localhost and username.
Like if you are using Xampp, in the host column type Localhost
and the database name
and last but not least the username with root, and then install.
It's quite easy but if you missing it, it's gone be very upsetting.

Categories