0

I have a ZK F7 attendance machine with TCP/IP port which returns data through the software comes with it. But I want to collect attendance data through php using php socket in an array then save them in mysql. to do this I tried the code bellow but getting erro (call to undefined function socket_create()). what should be the solve of the error?

<?php

// Server IP address
$address = "192.168.1.201";

// Port to listen
$port = 4370;

$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock,$address, $port) or die('Could not bind to address');
socket_listen($mysock, 5);
$client = socket_accept($mysock);

// read 1024 bytes from client
$input = socket_read($client, 1024);

// write received gprs data to the file
writeToFile('socketLog.txt', $input);

socket_close($client);
socket_close($mysock);

function writeToFile($strFilename, $strText) {
    if($fp = @fopen($strFilename,"w")) {
        $contents = fwrite($fp, $strText);
        fclose($fp);
        return true;
    } else {
        return false;
    }
}
?>
kero
  • 10,647
  • 5
  • 41
  • 51
Abdur Rahim
  • 41
  • 2
  • 10
  • 2
    make sure you have the package installed: http://stackoverflow.com/questions/6137823/fatal-error-call-to-undefined-function-socket-create – Cristian Cavalli Nov 10 '13 at 01:52
  • Enabled the extension=php_sockets.dll now the error is: "socket_bind(): unable to bind address [10049]: The requested address is not valid in its context." my device ip is 192.168.1.201 and the port is 4370. what to do now? thanks in advance.... – Abdur Rahim Nov 10 '13 at 10:06
  • You can't bind to another machine other than a local-host. It looks as if you are trying but I am not sure [the ip is not 127.0.0.1 or some variant thereof and I think you describe it as your 'device ip' not your host ip], check out this article: http://objectmix.com/php/454591-sockets-requested-address-not-valid-its-context.html Anyway you have to bind to your localhost and listen or send, not to another machine. – Cristian Cavalli Nov 10 '13 at 17:57
  • My ZK F7 attendance machine is connected to my pc through Ethernet (RJ45) in the same network (192.168.1.201) and the machine's port is 4370. I want to get the attendance data from the machine through TCP/IP. in this case what can I do? – Abdur Rahim Nov 10 '13 at 19:09
  • Create a socket on your server (where the php interpreter is) and wait for input on the same socket config that your device is set too. If you need to send input from your server to the device check out this: http://php.net/manual/en/function.socket-send.php – Cristian Cavalli Nov 11 '13 at 01:08
  • @AbdurRahimAbhi I have exactly same application to do. I have a attendance machine connected to local network and I want to read attendance data from the machine. So just wanted to check if you are able to do any solution for the same? Please help? Thanks – SachinKRaj Sep 19 '15 at 08:32

2 Answers2

1

You don't have php_sockets extension enabled. Go do that in php.ini

dorphalsig
  • 689
  • 1
  • 10
  • 27
  • or the package may not be installed! – Cristian Cavalli Nov 10 '13 at 02:08
  • Enabled the extension=php_sockets.dll now the error is: "socket_bind(): unable to bind address [10049]: The requested address is not valid in its context." my device ip is 192.168.1.201 and the port is 4370. what to do now? thanks in advance.... – Abdur Rahim Nov 10 '13 at 10:06
0

Change the line

$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

to

$mysock = socket_create(AF_INET, SOCK_DGRAM, 0);

And I guess that will fix it by using UDP to connect to the ZK device.

user247702
  • 23,641
  • 15
  • 110
  • 157
joe mwirigi
  • 471
  • 8
  • 9