Problematic fsockopen snippet...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Problematic fsockopen snippet...

Post by tomfra »

I am using a PHP script that I did not write which uses this fsockopen snippet to do the workn in "background":

Code: Select all

$op = fsockopen($_SERVER['HTTP_HOST'], 80);
			if (!$op) {
			  echo "$errstr ($errno)<br>\n";
			} else {

				fputs($op, "GET ". $URL . "\r\n\r\n");
				if ($dispoutput) { while (!feof($op)) { echo fread($op, 1024); } }
				fclose($op);
			}
It works, but it does not seem to be reliable enough. I'll gladly replace it with CURL if it solves the reliability problem. Unfortunately, I have very little experience with CURL.

So the questions are:

Do you spot any possible problem with the above fsockopen code that could result in low reliability (I think it has something to do with incorrent connection closing but that could be pure speculation).

Do you know how to accomplish the same thing with CURL?

Thanks!

Tomas
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The problem is not fsockopen.. But the pseudo HTTP that is sent to the webserver..

I'm pretty sure this code will result into problems if you have a webserver with different virtual hosts..

You could use something like:

Code: Select all

$data = file_get_contents('http://blah.com/foo');
Post Reply