replace fopen with curl

Hello folks,

my server does not support fopen i was getting errors when I deployed a site with external urls like so:

[php]include “http://dl.dropbox.com/u/21309836/weekdays.txt”;[/php]

so I tried replacing them with curl, and low and behold it worked, the external files data was visible.
But it was sooooooooooooooooooooooooooooooooooooooo slowwwww. How can I make this faster.
There were about 20 external links inside a switch, only 2 different urls. Maybe use only 2 curl instances, and
then put that into the switch 20 times, so we don’t open 20 curls?

[php]<?php
$url = “http://www.domain.com/”;
$data = “”;
$cp = curl_init();
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cp, CURLOPT_URL, $url);
curl_setopt($cp, CURLOPT_TIMEOUT, 60);
$data = curl_exec($cp);
curl_close($cp);
echo $data;
?>
[/php]

the whole switch looks like this:

[php]<?php switch ($lng) {

	case null:

	echo (" <span class='bold'>Bed </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekdays.txt";
	echo (" euros p.p - Weekdays (Sunday to Thursday)<br />
								  <span class='bold'>Bed </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekends.txt";
	echo (" euros p.p - Weekends (Friday and Saturday)<br />
								  <span class='bold'>Dorms </span>- 6 8 & 10 persons<br />

Check-in - 12 pm
");

	break;

	case 1:

	echo (" <span class='bold'>Bed </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekdays.txt";
	echo (" euros p.p - Weekdays (Sunday to Thursday)<br />
								  <span class='bold'>Bed </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekends.txt";
	echo (" euros p.p - Weekends (Friday and Saturday)<br />
								  <span class='bold'>Dorms </span>- 6 8 & 10 persons<br />

Check-in - 12 pm
");

	break;

	case 2:

	echo (" <span class='bold'>Cama </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekdays.txt";
	echo (" euros p.p - Dias da semana (Domingo a Quinta)<br />
								  <span class='bold'>Cama </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekends.txt";
	echo (" euros p.p - Fins de Semana (Sextas e Sábados)<br />
								  <span class='bold'>Dormitórios </span>- 6 8 & 10 pessoas<br />

Check-in - 12 pm
");

	break;

	case 3:

	echo (" <span class='bold'>Lit </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekdays.txt";
	echo (" euros p.p - Jours de Semaine (Dimanche &#224; Jeudi)<br />
								  <span class='bold'>Lit </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekends.txt";
	echo (" euros p.p - Weekend (Vendredi et Samedi)<br />
								  <span class='bold'>Dortoir </span>- 6 8 & 10 persones<br />

Check-in - 12 pm
");

	break;

	case 4:

	echo (" <span class='bold'>Cama </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekdays.txt";
	echo (" euros p.p - Dias de Semana (Domingo hasta Jueves)<br />
								  <span class='bold'>Cama </span>- ");
	include "http://dl.dropbox.com/u/21309836/weekends.txt";
	echo (" euros p.p - Fin de Semana (Viernes y Sabado)<br />
								  <span class='bold'>Dormitorios </span>- 6 8 & 10 personas<br />

Check-in - 12 pm
");

	break;

			

	}?>[/php]

This is an fopen problem no?

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/davidtesta/domains/wijbouwensites.nl/public_html/andarilhohostel/pricing.php on line 165

or isn’t it?

I tried this instead of curl:

[php]$a = file_get_contents(“http://dl.dropbox.com/u/21309836/weekdays.txt”);
echo ($a);[/php]

but now I get a timeout like this:

Warning: file_get_contents(http://dl.dropbox.com/u/21309836/weekdays.txt) [function.file-get-contents]: failed to open stream: Connection timed out in /home/davidtesta/domains/wijbouwensites.nl/public_html/andarilhohostel/pricing.php on line 184

sometimes it works, sometimes a timeout, it;s really slow,

yes fopen needs to have the file on the same server - not elsewhere. thats when you would use curl

I tried curl and I tried file_get_contents, both methods WORK but are incredibly SLOW (minutes rather then seconds) just for getting the number 12 from a textfile on a remote server.
Why is it so slow? anybody?

thanks in advance!!

Well, is this YOUR domain: http://dl.dropbox.com/

Most sites or HOSTS do not allow cross-domain includes. That opens all kinds of doors for hackers to break into your code. They just have to redirect the second domain and load all kinds of crap onto your server…

If it is YOUR site, then, just drop the domain name and use the actual folder/file structure.

Otherwise, here is a way to access across domains. I have not tested it all but, looks like it might work for you.
http://alvinabad.wordpress.com/2009/02/13/feb13/

Good luck…

Yes it is our domain but it is remore from the site, curl and get contents both work but slow. SLOW.
so it is not blocked by crossdomain policy.
actually we are just taking 2 number inside the website, these are prices that change, so somebody at the reception edits the 2 textfiles, save in the dropbox and that get synced with the online textfiles.

So the question is WHY is curl and get_filecontents so slow in my case.

So, you own the other address, but it is another domain. So, that is CROSS-domain access.
This can be a problem if you have not set up any crossdomain parms for the servers to allow this access.
But, I found this sample of how to do it with CURL. It is actually for a flash-text-display routine to pull
XML files, but, should work for text files too. Look down to the PHP code for the CURL routine to do the
pull of a xml file from the other domain. Then, change it to your text info…
http://nickhardeman.com/146/loading-an-xml-file-from-another-domain-without-a-cross-domain-policy-file/
Hope that helps!

Thanks foir helping me , I agree with you that this definitely is cross domain, but I thought it is either blocked or it works, but slowing it down, can this be a cross domain issue?
I have had cross domain issues in the past with flash, where the connection gets blocked but never slowed down. Then with the help of a trusted domain file it is unblocked.
I am going to look into your soulutions, to make sure it is not a cross domain issue that slows down curl and get-content. thanks will let you know.

Well, also, I have seen where sometimes there is a firewall or anti-virus system that is checking all of the files first and delays the posting of them. I am assuming that you are using a program on one server calling files on another server and not using a computer for one of these. Server to server is either a cross-domain issue or could be an authentication issue. (Permissions, etc…) Not sure, but, try that curl code on that link. It seems to work for them. Good luck.

Thats just the curl option, in this case I tried that allready, it works, but slow

Okay, I would guess it’s some option that is set wrong…
When you run this version, what happens? (just a new php page with this inside the tags)
[php]

<?php // create a new cURL resource $cp = curl_init(); // set URL and other appropriate options curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1); curl_setopt($cp, CURLOPT_URL,"http://dl.dropbox.com/u/21309836/weekdays.txt"); curl_setopt($cp, CURLOPT_HEADER, 0); // grab URL and pass it to the browser $data = curl_exec($cp); // close cURL resource, and free up system resources curl_close($cp); // display the data... echo "curl data: " . $data . "

"; $a = file_get_contents("http://dl.dropbox.com/u/21309836/weekdays.txt"); echo "get_contents data: " . $a; ?>

[/php]
I put this on a file on my server… (GoDaddy.com server) and it displays the results very quickly:

curl data: 13

get_contents data: 13 

So, it does retrieve data and seems to get it very quickly.
I would guess that the timeout option you used is incorrect. Hope that fixes it!

Sponsor our Newsletter | Privacy Policy | Terms of Service