PHP fwrite() to socket - operation not permitted? - php

I'm making a simple utility in PHP to control my Minecraft server via UDP:
$fp = fsockopen('udp://host', 'port', $errno, $errstr);
if (!$fp)
error("Unable to connect!");
else {
fwrite($fp, $data['command'].':user:pass');
stream_set_timeout($fp, 5);
error(fread($fp, 128));
fclose($fp);
}
For some reason fwrite is throwing this error:
Notice: fwrite() [function.fwrite]: send of 20 bytes failed with errno=1 Operation not permitted in /homepages/44/d217581656/htdocs/xenforo/util/remoterestart/interface.php on line 22
Anyone know why?

I'm going to go out on a limb and say you're probably not permitted to use sockets on your server. This is a fairly common thing to disable on most hosts.
Run a phpinfo() and see if there is any socket functions which are disabled, or contact your host and just ask.

Related

I'm getting "Unable to find the socket transport 'http'" error suddenly. It was working fine until we switched networks

After a network switch, parts of my program that send data to other servers are no longer working.
I tried the following code:
<?php
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
if(!$fp) {
echo "Error: $errstr ($errno)<br>\n";
} else {
fputs($fp,"GET / HTTP/1.0\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
After running that code, I am presented with the following error:
Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (19)
What do I need to check to ensure this works? It's baffling because it was working fine just before switching networks. I'm also getting the "php_network_getaddresses: getaddrinfo" error when I try get_file_contents.
Did you try opening the socket without the protocol part, e.g. just
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
I found the answer by doing a google search for
Unable to find the socket transport "http"
The same answer is in all of the top 5 results, so it would have saved you 3 days to just spend 5 seconds copying and pasting the error in to google.

Php stream_socket_client - Only one packet is sent

I have got a client server application using TLS and C# and I have a requirement to make an extension with PHP.
I am able to establish a connection and receive data once, after that, the server doesn't receive anything else and the php script reaches the end.
My PHP code is as follow:
$fp = stream_socket_client("tls://localhost:1776", $errno, $errstr, 30);
if (!$fp) {
die("Unable to connect: $errstr ($errno)");
}
fwrite($fp, "Test1");
fwrite($fp, "Test2");
fclose($fp);
echo "DONE!";
I have attempted several approaches but the second fwrite never reaches the server no matter what. I am 100% sure the server is not the problem as the code is exactly the same for the C# client and that one works flawless.
Could anyone give me any tips on how to overcome this?
Thanks for reading and best regards.

I can't use Paypal IPN on my Hosting, how to solve this?

I'm testing paypal ipn for a website I am building, but it fails when I try to validate the ipn. In particular this line doesn't work:
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
the check if (!$fp) fails (fp is false). I assume it can't connect. Why this?
Update
I discovered my hosting solution doesn't support ssl, in fact I get:
Warning: fsockopen() [function.fsockopen]: unable to connect to
ssl://www.sandbox.paypal.com:443 (Unable to find the socket transport
"ssl" - did you forget to enable it when you configured PHP?)
Is there a way to get over this without going for another hosting solution?
You don't have to use SSL (though Paypal highly recommends it.)
$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);

fsockopen is returning an error even if the server im trying to reach is online?

im using fsockopen below:
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 1);
return ($errno === 0);
Now this should return either false or true if the server is up or not. I know this server is definty up yet i still keep getting an error
Warning: fsockopen() [function.fsockopen]: unable to connect to uberminecraft.com:25565 (Connection timed out)
You have set a timeout of 1 second, is this intentional? Do you know whether the server is able to provide a response fast enough? Have you tried setting a higher timeout?
Also you might want to look at php.net for how to check whether the connection was made.
You must change fsockopen function as follow
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 30);
Still you are getting same error. please tell to your host provider to open 25565 port.
Thanks

iPhone SDK Push notification randomly fails

I have a PHP file with the following content that works perfectly on development ceritficates, but when I switch to a production certificate the PHP errors and gives the below message, but it only does this about 50% of the time. The other 50% it works. Anyone know why this might be happening?
<?php
// masked for security reason
$deviceToken = 'xxxxxx'; // jq
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__)."/prod.pem");
$number = 5;
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr\n";
}
else {
print "Connection OK\n";
$msg = $_GET['msg'];
$payload['aps'] = array('alert' => $msg, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";
fwrite($fp, $msg);
fclose($fp);
}
?>
The PHP error:
Warning: stream_socket_client() [function.stream-socket-client]: Unable to set local cert chain file `/var/www/vhosts/thissite.com/httpdocs/prod.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: failed to create an SSL handle in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /var/www/vhosts/thissite.com/httpdocs/pushMessageLive.php on line 19
Failed to connect 0
I had the same issue. You have to make a persistent socket connection with Apple's Push Notification Server. I've written up a tutorial for a python daemon called pyapns (http://github.com/samuraisam/pyapns) which worked very well for me:
http://www.how2s.org/index.php/How_to_get_started_with_Apple_Push_Notifications_for_iPhone_or_iPhone_Touch
This works assuming you are running Debian and have root access to install the required packages such as python-twisted, libcurl4-openssl-dev etc.
Sounds like too many connects. Apple's docs state that you need to hold the connection open and send as many as you can at the same time. Re-opening is considered DOS attack. So try making it persistent and see if you get same error.
I don't know if the error you are experiencing are because of too many connects to the push servers... In my experience, those limits are a bit hard to reach.
But PHP on the other hand have been acting strange when I've tried to send batches of push notifications. I'm not sure from your sample code, but I guess you do a stream_socket_client() and fclose() for every message? Using that technique with SSL sockets in PHP, the only thing I've personally have accomplished is failure...
I'm not sure if you have the possibility to run Ruby on your server, but if you can, I recommend switching to ruby-apns-daemon to handle the talk with Apple's servers. It's lightweight and easy to implement in PHP (you practically compose the same payload-JSON, but send it to ruby-apns-daemon instead of through a socket).
I've had the same issue and certificate was in fault. You can see solutions here How can I do an SSL connection with PHP and here Error using ssl cert with PHP.
Hope it'll help you.
And for the record you are not obliged to make a persistent connection with APNS. Though it's best to send all your messages at once, you can connect and disconnect multiple times. I quote Apple's website :
You should also retain connections
with APNs across multiple
notifications. APNs may consider
connections that are rapidly and
repeatedly established and torn down
as a denial-of-service attack. Upon
error, APNs closes the connection on
which the error occurred.
If you don't create hundred of connections at a time you should not get troubles.

Categories