I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo 'Error: '.$errno.', '.$errstr;
} else {
fwrite($fp, 'kool');
}
There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false
This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.
You should also print the error message(s) for debugging
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo "Error No: ".$errno."<br />\n";
echo "Error Str: ".$errstr."<br />\n";
} else {
fwrite($fp, 'kool');
}
If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.
Related
How can I understand the error when trying to open a socket?
I get error 0, however all SSL certificate works fine.
$fp = fsockopen("ssl://cloud-messaging.bitrix24.com", 443, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n: " . $errno;
print_r($errstr);
} else {
fclose($fp);
}
Result:
ERROR: 0 -
: 0
But it works when I try to open the socket with myself.
Maybe there is some script that will allow you to get at least some error?
I have this piece of code:
$fp = fopen("/path/to/file", "a+");
if (!$fp) {
fwrite($fp, "somedata\n");
fclose($fp);
} else {
echo "cannot open";
}
and the result is an empty file and the message cannot open. I have SELinux disabled and the permissions seem OK, otherwise the file wouldn't be created, I'd say.
Any ideas?
Obvious mistake:
if (!$fp) {
should be
if ($fp) {
I got a file on a gameserver called "current_map.tmp".
This file contains a number depending on the current map.
What I need is to read that number.
This is what I got so far:
<?php
$server_ip = '213.239.207.85';
$server_port = 27960;
$server_timeout = 2;
$server_addr = "udp://" . $server_ip;
$fp = fsockopen($server_addr, $server_port, $errno, $errstr, $server_timeout);
socket_set_timeout ($fp, $server_timeout);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
$File = "current_map.tmp";
$filesize = filesize($File);
$handle = fopen($File, "r");
$map_id = fread($handle, $filesize);
fclose($handle);
}
fclose($fp);
?>
$fp returns "Resource id #2".
So that works.
Then there is nothing.
1) How do I know wich folder I connected to with $fp?
2) How can I read the content of this file?
$fp returns "Resource id #2". So that works.
No; this doesn't actually mean anything! Since UDP sockets are connectionless, there is no such thing as a UDP "connection"; calling fsockopen() only initializes sockets to prepare to send packets.
In any case, sending and receiving UDP packets does not allow you to access files on a remote server, unless that server has implemented a protocol to allow you to do so, and you are making use of that protocol. It certainly will not allow you to use fopen() to access remote files — this code is essentially just nonsense.
I'm trying to use fsockopen to communicate with a game server, which responds with some basic stats. It works perfectly when the server is online, but if the server is ever offline, the following code causes php to stop displaying the page that reads the data.
try {
$socket = fsockopen($host, $port, $errno, $errstr, 10);
if ($socket !== false) {
fwrite($socket, "\xFE");
$data = "";
$data = fread($socket, 1024);
fclose($socket);
if ($data !== false && substr($data, 0, 1) == "\xFF") {
// get into
} else {
// Server did not send back proper data, or reading from socket failed.
print "Server not available.";
}
} else {
// ...
}
} catch(Exception $e){
// ...
}
I've tried the try/catch, I've tried adding a custom handler to the exception. My only idea is to run this outside of the web requests and store the response so that the web request isn't initiating it.
Any thoughts?
First, I'd add a couple of echo commands, either side of the fsockopen call:
echo date("Y-m-d H:i:s")."Before open\n";
$socket = fsockopen($host, $port, $errno, $errstr, 10);
echo date("Y-m-d H:i:s")."After open (socket=".($socket===false?"Bad":"OK")."\n";
This is to confirm the 10 second timeout is working. If you never see the second message then the timeout is not working, and the problem is more obscure.
Anyway, if you are getting a valid $socket, but the lock-up happens later, then try:
if ($socket !== false) {
stream_set_timeout($socket,2); //2 second timeout
stream_set_blocking($socket,false); //no blocking
fwrite($socket, "\xFE");
...
P.S. If adding those two commands solves the problem, then experiment to see if just one of them solves it. That would give a big clue what the real problem is.
It seems that by moving the logic outside the html generation worked. The lookup happens before any html is rendered, so if it fails it doesn't interrupt the html output.
I'm trying to make a simple UDP client server example in PHP but I face an error.
This is the client :
$fp = stream_socket_client("udp://192.168.0.12:12478", $errno, $errstr);
if ($fp)
{
fwrite($fp, "TEST 1 TEST 2 TEST 3");
$buf = fgets($fp);
var_dump($buf);
fclose($fp);
}
This is the server :
$socket = stream_socket_server("udp://192.168.0.12:12478", $errno, $errstr, STREAM_SERVER_BIND);
if ($socket)
{
while ($conn = stream_socket_accept($socket)) {
fwrite($conn, date("D M j H:i:s Y\r\n"));
fclose($conn);
}
fclose($socket);
}
All executions end with :
Warning: stream_socket_accept(): accept failed: Operation not supported
Basically, this is the example given in all PHP documentations but I can't figure what is wrong in it. Any help is greatly appreciated.
Thanks.
Here is the warning on the very same page
Warning
This function should not be used with UDP server sockets. Instead,
use stream_socket_recvfrom() and
stream_socket_sendto().
according to the documentation: "you cannot make a silk piurse from a sow's ear"
stream_socket_connect is intended for STREAMS, not datagram packets. recvfrom would be more likely to work in this scenario.