file_get_contents failed to open stream with a valid url - php

I'm trying to use this code in a php file.
I want get the source code from this url and parse the content.
<?php
$fuente = file_get_contents('http://www.akiracomics.com');
echo $fuente;
?>
The problem is, after execute the code I received this error
Warning: file_get_contents(http://www.akiracomics.com) [function.file-get-contents]: failed to open stream: Connection timed out in XXXXXX/test.php on line 2
I tried from the same server to other url and works perfect.
Any idea?
Thanx

It's working here, probably you're making too many requests (what are you trying to do, by the way?) and they are blocking incoming requests from your server.

I finally found the solution. I need to use a curl_exec instead file_get_contents.
With this code, everything works fine.
$curlIMG = curl_init();
curl_setopt($curlIMG, CURLOPT_URL, "http://www.google.es");
curl_setopt($curlIMG, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlIMG, CURLOPT_HEADER, false);
$imgBinary = curl_exec($curlIMG);
curl_close($curlIMG);

Related

simplexml_load_file() Connection timed out trying to parse remote xml

I am trying to parse an xml file from this url
http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220
The ref number expires daily so you may see almost no content by the time you look into it. But this is urelated with the issue.
I am trying to parse it with simplexml_load_file() using
<?php
$xml = 'http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220';
$xml= simplexml_load_file($xml) or die("Error: Cannot create object");
echo urldecode($xml->hoteldescription); ?>
Unsuccessfully it returns 'Connection timed out'.
But when I try to do the same with a random xml url I found on the net the action completes just fine:
<?php $xml=simplexml_load_file('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V') or die("Error: Cannot create object");
echo $xml->Time['TimeStamp']?>
Why I am getting the Connection Time Out? The providers of the link said that it is a simple task. If I download the xml source from the url on a local file on my server I can fetch it with no problems. So there must be something with the remote request.
Needless to say that I am a php newbie... especially on server requests.
Thanks!
EDIT
I have also tried file_get_contents() and it didn't work for me. Also the last thing I tried again with no success was this
function load_file_from_url($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'mydomain');
$str = curl_exec($curl);
curl_close($curl);
return $str;
}
function load_xml_from_url($url) {
return simplexml_load_string(load_file_from_url($url));
}
$xml = load_xml_from_url('http://87.102.127.86:8081/kwiktext/kwiktext.exe?Page=52&compid=33&refnum=ICE2-1713464-220');'
EDIT 2
Apparently the 8081 port that was required from the service was closed on both the servers I was attempting connection by the hosts.
This works fine for me. I copied and pasted your code and ran it locally without any problems. If you encountered a connection timeout error it would seem to be a temporary error. The only error I noticed was when you are printing the hotel description the property name was incorrect:
echo urldecode($xml->HotelDescription);

Just call a https URL with PHP (without waiting for response)

I already googled and searched for an answer, and found also one, but I still get an error...
I just want to start a cronjob by calling a certain URL but I get this failure message:
PHP Warning: fopen(https://my-url.de): failed to open stream: HTTP request failed! in /var/www/vhosts/httpdocs/cronjob/import.php on line 41
I tried this one:
ignore_user_abort(true);
set_time_limit(0);
But it is not working... The url triggers a cronjob but this one needs to long to response...
So what would you suggest to call a cronjob in PHP?
I must call the cronjob by URL and I do not need to get a response!
Greetings and Thank You!
you can use curl instead of fopen as follows
$url = 'https://my-url.de';
$ch = curl_init($url);
// this will prevent printing out the contents , unless you are make echo $content
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

how to stop & from changing to &amp in a url?

I have a url like this, https://www.example.com/send?id=1&text=http://example2.com/song.mp3
when I try to get the data of the url using file_get_contents() it shows below error,
PHP Warning: file_get_contents(https://www.example.com/send?id=1&amptext=http://example2.com/song.mp3):
failed to open stream: HTTP request failed! HTTP/1.1 501 Not Implemented
and the & have converted to &amp. due to that the url is not working.
I have tried htmlspecialchars_decode() and preg_replace() on & , but it didn't do anything.
how to solve this ?
I think you better to use cURL instead of file_get_contents(), because referring server must be enabled allow_url_fopen in their server if you are using file_get_contents(), but cURL is a library it just make a http requests
<?php
$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"https://www.example.com/send?id=1&text=http://example2.com/song.mp3");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>
refer following links
file_get_contents script works with some websites but not others
cURL vs file_get_contents
take a look at this sir, where $str="your link"
sample
$str = "https://www.example.com/send?id=1&text=http://example2.com/song.mp3";
echo html_entity_decode($str);
will output the one you want....

Simple html dom not working for a table[class]

I want to use the contents of a website and include them in mine with "Simple Html DOM", unfortunately the code which normally works for another websites, fails in this specific case:
<?php
// Note you must download the php files from the link above
// and link to them on this line below.
include_once('simple_html_dom.php');
$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;
?>
Warning:
file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.1 500 Internal Server Error in
public_html/elin-custom-code/simple_html_dom.php on
line 75
Fatal error: Call to a member function find() on a non-object in
public_html/elin-custom-code/sklad-1.php on line 9
As a link for the reference:-
file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.
You can use CURL or a PHP client library like Guzzle.
To be make sure i just use CURL to check what is the exact problem with the help of below code:-
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>
Output:- http://prntscr.com/a91nl5
So it seems that for security reason these method calls(CURL and file_get_contents) are blocked on that site.
You can try this link answers also, may be you will get some useful output:-
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
Good Luck.

This curl command doesn't work for some reason

I have a URL that I need to "call" from a PHP script. The URL makes a phone call using Tropo's API, so it's easy to verify if it was called.
My PHP looks like this:
<?php
$oid=$_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
echo $notify_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $notify_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
mail('me#gmail.com', 'cURL', "Did a cURL");
?>
This prints out the $notify_url variable, and when I take the printed value and enter it into a browser window, I get the desired result i.e. the phone call to my phone.
I have tried this on two web hosts that claim they support CURL, one is paid (crazydomains.com.ayu - just got off the phone to support) and the other is 000webhost.com
Am I doing something wrong? This one is kind of confusing, since it should be so simple.
EDIT: I receive the mail as expected.
EDIT 2: If you have any ideas about how I can debug this, I would appreciate it.
EDIT 3: As Juhana suggested I added echo curl_error(); after curl_exec and I got this error ...
Warning: Wrong parameter count for curl_error() in /home/a5352876/public_html/curl.php on line 15
EDIT 4: changed the echo curl_error() to echo curl_error($ch) and got the message couldn't connect to host so that seems to be the problem.
Now the question is, why can't it connect to a host that is easily accessible through a browser, is there anywhere I can look for that?
If your host has HTTP wrappers enabled, and the allow_url_fopen config option is enabled, then you don't need cURL, especially for such a simple request.
It looks like all you need to do is open a URL, which you can easily do with fopen():
$oid = $_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
$fh = fopen( $notify_url, 'r');
fclose( $fh);

Categories