Fsockopen

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
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Fsockopen

Post by agriz »

Hi,

I m trying to fetch data from fsockopen.

I am using PHP file which do some php and mysql and outputs xml data.

When i use fsockopen and php file,

i am always getting
4a XML DATA 0
Instead of PHP file, If i call directly a static XML file, It works fine.

Code: Select all

ini_set('display_errors',0);
set_time_limit(0);
header("Content-type: text/xml");
Then some php and mysql coding and then

Code: Select all

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
	$xml .= '<totals>';
	$xml .= '<total>'.$total_pages.'</total>';
	$xml .= '</totals>';
	
	echo $xml;
Any problem using like that?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Fsockopen

Post by John Cartwright »

Post that code that actually deals with fsockopen()
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Fsockopen

Post by agriz »

Code: Select all

if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
	{
		@fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
		@fputs($fsock, "HOST: $host\r\n");
		@fputs($fsock, "Connection: Close\r\n\r\n");

		$file_info = '';
		$get_info = false;

		while (!@feof($fsock))
		{
			if ($get_info)
			{
				$file_info .= @fread($fsock, 1024);
			}
			else
			{
				$line = @fgets($fsock, 1024);
				if ($line == "\r\n")
				{
					$get_info = true;
				}
				else if (stripos($line, '404 not found') !== false)
				{
					$errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename;
					return false;
				}
			}
		}
		@fclose($fsock);
	}
	else
	{
		if ($errstr)
		{
			return false;
		}
		else
		{
			return false;
		}
	}

	return $file_info;
When the very first time, It works.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Fsockopen

Post by John Cartwright »

When the very first time, It works.
I don't understand what this means. Since I still don't really know your issue, let me suggest removing all the @ in your code to see if any errors are being generated.
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Fsockopen

Post by agriz »

I removed the @
There is no error.

This is what happening. From fsockopen, I am trying to open a PHP file which outputs XML

I am storing the file content in a variable. And if try to echo the variable,

If i restart apache, I am getting 17 (view source gives complete xml_data)
When i refresh the browser now, I am getting 4a 17 0 (view source gives complete 4a xml_data 0)

Now instead of PHP file, (I have created a static XML file in the same place. ) If i call xml file, at any number of refresh gives 17.
I am not able to predict whether there is a problem in localhost or from live server.

I am that php file which produce xml in live server. and i am testing fsockopen from my localhost.

Apache restart gives me a clue that problem in localhost.
When Calling directly static XML file from live server tells me there is a problem in PHP file which produce xml.

I am blank now.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Fsockopen

Post by John Cartwright »

What about if you used the cURL library? Or file_get_contents()?

I.e.,

Code: Select all

$xmlContent = file_get_contents('http://'. $host .'/'. $directory .'/'. $filename);
or

Code: Select all

$ch = curl_init('http://'. $host .'/'. $directory .'/'. $filename); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlContent = curl_exec($ch);
Just to be clear, those odd characters are the result of you not parsing the HTTP response correctly. They are used for chunked-encoding, if memory serves (which is why I recommend you do not attempt to parse responses, and preferably use cURL).
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Fsockopen

Post by agriz »

It is kind of script which will get data from my database. and the script is free for few people.
* Is using Curl a safe method?
* Generally every server will have curl enabled?
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Fsockopen

Post by agriz »

I tried curl method which is working correctly.
Please let me know is this safe to use curl.?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Fsockopen

Post by Weirdan »

Your original problem was that server was sending the reply in chunked transfer encoding and you didn't decode it. Curl does that behind the scene.

curl library is usually available on most hosting plans nowadays.
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Fsockopen

Post by agriz »

Weirdan wrote:Your original problem was that server was sending the reply in chunked transfer encoding and you didn't decode it. Curl does that behind the scene.

curl library is usually available on most hosting plans nowadays.
Okay. I will go ahead with CURL.
Is it difficult to decode it?
Post Reply