I need help with code that can receive TCP data into a PHP file
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi friends

I'm receiving data from an arduino hardware device and the manufacturer says he has set it up to post to my server via a URL.

How can I read the data using PHP?

He says on the devices' logs the data is like this
$$0121AAKCQ 007A|128008001401010101013812000000000000000000000000100C25A2000000000000.000000000000.0000N00000.0000E00116D

How can I get this data via PHP? Is there PHP code that reads TCP data? Is it read via POST or via GET or via REQUEST or via sockets?

Please help a newbie


Have you seen this thread? https://arduino.stackexchange.com/questions/28481/post-data-from-arduino-to-website Looks to me like a good start.
kostasx 9 months ago
Hi jobokoth, have you worked with sockets before ?
SilverHood Apps 9 months ago
No, I haven't worked with sockets before
jobokoth 9 months ago
Well.. I think you'll be needing that.. may I know to what IP address and port does your Arduino points to?
SilverHood Apps 9 months ago
Its posting to a domain name port 80
jobokoth 9 months ago
Ok i have a code in handly.. I'll modify and post it according to your need.. Since I do not have the environment setup for testing I'll need your help in testing, together this will get complete if thats ok with you? I'll be with you the whole time
SilverHood Apps 9 months ago
Hi, any progress??
SilverHood Apps 9 months ago
hi, just to understand your requirements, are you trying to get the arduino host to do a HTTP POST to your PHP script? what are you trying to do with the data once it is posted, are there any details on the format of the data "$$01......00116D"?
kush 9 months ago
Yes Kush, you are right, once the data is posted, I'll insert it into my database as normal
jobokoth 8 months ago

Crowdsource coding tasks.

1 Solution

Hi, the main part here is to read

$buf = socket_read($msgsock, 2048, PHP_NORMAL_READ)
echo $buf

This is put into an infinite loop so that it keeps listening to the socket inside


Complete code, do test and let me know..


/* Allow the script to hang around waiting for connections. */


/* Turn on implicit output flushing so we see what we’re getting

* as it comes in. */


$hostname = "www.example.com";

$address = gethostbyname($hostname); //eg. ‘’;

$port = 80;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {

    echo “socket_create() failed: reason:  . socket_strerror(socket_last_error()) . “\n”;


if (socket_bind($sock, $address, $port) === false) {

    echo “socket_bind() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;


if (socket_listen($sock, 5) === false) {

    echo “socket_listen() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;


do {

    if (($msgsock = socket_accept($sock)) === false) {

        echo “socket_accept() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;



    do {

        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {

            echo “socket_read() failed: reason:  . socket_strerror(socket_last_error($msgsock)) . “\n”;

            break 2;

        //trim the message
        if (!$buf = trim($buf)) {

        //if device said quite then exit
        if ($buf == 'quit') {

        //if server needs to be shut down
        if ($buf == 'shutdown') {
            break 2;

        //Use only for taking back to your device

        //$talkback = “PHP: You said ‘$buf’.\n”;

        //socket_write($msgsock, $talkback, strlen($talkback));

        echo $buf\n”;

    } while (true);


} while (true);


Hi Silverhood, I'll be testing later today and give you feedback. Thanks
jobokoth 9 months ago
Alrighty 👍
SilverHood Apps 9 months ago
Hi I have implemented the code but its not picking the data.... Or can we send to your server? Job
jobokoth 9 months ago
Hi job, did you receive my private message?
SilverHood Apps 9 months ago