I'm completely new to PHP sockets and I'm gonna use them for one simple purpose at the moment: I want to pass information between a Cron Job and the net one, and since my host prevents me from using putenv
, this is the best solution I found.
Reading the official documentation, this is what I tried to do:
$host = "localhost"; //or ssl://mydomain.com
$socket = fsockopen($host, 80, $no, $err,0);
if(!$err)
{
var_dump(fputs($socket, "random text"));
var_dump(filesize($socket));
var_dump(fgets($socket, filesize($socket)));
fclose($socket);
}
This is the output I get:
int(3)
NULL
bool(false)
So it seems that fput
has success, but for some reason it does not actually write anything (same result with fwrite
and fread
.
UPDATE:
for anyone interested in that, I found out another solution which uses a completely different approach.
Now I just have to test it out; if it does not work for any reason, I'm gonna proceed with peakle's solution
you have some errors on your script, like: zero timeout in fsockopen, provide resource
to filesize
function, i fixed some them and if your server on localhost
domain works fine it will be output correct response:
<?php
$host = 'localhost';
$socket = fsockopen($host, 80, $no, $err, 30);
if (!$err) {
var_dump(fputs($socket, "random text"));
while (!feof($socket)) {
echo fgets($socket, 4096);
}
fclose($socket);
}
:)