Page 1 of 1
Fsockopen
Posted: Thu Mar 03, 2011 1:08 pm
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?
Re: Fsockopen
Posted: Thu Mar 03, 2011 1:58 pm
by John Cartwright
Post that code that actually deals with fsockopen()
Re: Fsockopen
Posted: Thu Mar 03, 2011 2:03 pm
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.
Re: Fsockopen
Posted: Thu Mar 03, 2011 2:14 pm
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.
Re: Fsockopen
Posted: Thu Mar 03, 2011 2:31 pm
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.
Re: Fsockopen
Posted: Thu Mar 03, 2011 3:07 pm
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).
Re: Fsockopen
Posted: Thu Mar 03, 2011 3:27 pm
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?
Re: Fsockopen
Posted: Thu Mar 03, 2011 3:49 pm
by agriz
I tried curl method which is working correctly.
Please let me know is this safe to use curl.?
Re: Fsockopen
Posted: Thu Mar 03, 2011 4:47 pm
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.
Re: Fsockopen
Posted: Thu Mar 03, 2011 4:55 pm
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?