Real-time php sockets

Background

The desktop application on the user's computer receives a phone number from the modem and sends it to a PHP script after receiving a phone call. At the moment, I can get data / package in the specified port through PHP. Then I have a scraper that connects to 411 databases and returns the address for the specified phone number.

Problem

After getting the phone number in PHP via sockets, how can I automatically refresh the parser 411 page with the new phone number?

code

socket_listener.php

set_time_limit(0); $address = "127.0.0.1"; $port = 10629; // create socket and bind with listener event $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($socket, $address, $port); socket_listen($socket); do { $accept = socket_accept($socket); $read = socket_read($accept, 1024) or die("Could not read input\n"); // parse phone number $phone = substr($read, 19, 15); $file = fopen("phone_numbers.txt", "a"); fwrite($file, $phone . "\r\n"); fclose($file); } while (true); 

phone_numbers.txt

 (425) 555-1212 (123) 456-7890 

Current solution

My current solution is pretty dodgy.

 modem -> desktop application -> socket_listener.php -> data.txt -> 411_scraper.php 
  • socket_listener.php listens on the port for incoming packets 24/7 and adds the new phone numbers that it receives to the text file
  • 411_scraper.php checks the text file for updates every 5 seconds. If the file version is changed, then it reads the last phone number
  • Run code to query 411.com and retrieve data by phone number

Desired Solution

  • socket_listener.php listens on the port for an incoming packet containing a phone number
  • The page automatically refreshes with new data received from 411

The things that I looked at

I looked at node.js, Ratchet (www.socketme.com) and Pusher ( http://pusher.com/ ), but they are all above my understanding. Pusher and Ratchet seem promising, but I haven't jumped into them yet.

+4
source share
1 answer

I would use ajax or something like that. PHP is not a reality. When you go to a site that uses PHP, the code is executed before the page is displayed. It processes the code on the server and returns the result as a string that you can send to the requesting client.

You will be storing data somewhere persistently. After executing the php script, the data execution for this is completed. If you are not using sessions.

-2
source

Source: https://habr.com/ru/post/1491040/


All Articles