src/Core.php
/**
     * This is the socket "equivalent" to the sendApiRequest function. However 
     * unlike that funciton it does not require the curl library to be 
     * installed, and will try to send/recieve information over a direct socket 
     * connection.
     *
     * @param string $host - who to send the request to
     * @param int $port - the port number to make the connection on.
     * @param Array $request - map of name/value pairs to send.
     * @param int $bufferSize - optionally define the size (num chars/bytes) of 
     *                          the buffer. If this is too small your 
     *                          information can get cut off, causing errors.
     *                          10485760 = 10 MiB
     * @param int $timeout - (optional, default 2) the number of seconds before 
     *                       connection attempt times out.
     * @param int $attemptsLimit - (optional, default 5) the number of failed 
     *                             connection attempts to make before giving up.
     * @return Array - the response from the api in name/value pairs.
     */
This is the socket "equivalent" to the sendApiRequest function. However
unlike that funciton it does not require the curl library to be
installed, and will try to send/recieve information over a direct socket
connection.

@param string $host - who to send the request to
@param int $port - the port number to make the connection on.
@param Array $request - map of name/value pairs to send.
@param int $bufferSize - optionally define the size (num chars/bytes) of
the buffer. If this is too small your
information can get cut off, causing errors.
10485760 = 10 MiB
@param int $timeout - (optional, default 2) the number of seconds before
connection attempt times out.
@param int $attemptsLimit - (optional, default 5) the number of failed
connection attempts to make before giving up.
@return Array - the response from the api in name/value pairs.
This is the socket " equivalent " to the sendApiRequest function . However unlike that funciton it does not require the curl library to be installed and will try to send / recieve information over a direct socket connection . @param string $host - who to send the request to @param int $port - the port number to make the connection on . @param Array $request - map of name / value pairs to send . @param int $bufferSize - optionally define the size ( num chars / bytes ) of the buffer . If this is too small your information can get cut off causing errors . 10485760 = 10 MiB @param int $timeout - ( optional default 2 ) the number of seconds before connection attempt times out . @param int $attemptsLimit - ( optional default 5 ) the number of failed connection attempts to make before giving up . @return Array - the response from the api in name / value pairs .
public static function sendTcpRequest($host, 
                                          $port, 
                                          $request,
                                          $bufferSize=10485760, 
                                          $timeout=2, 
                                          $attemptsLimit=100)
    {
        $reqString = json_encode($request) . PHP_EOL;
        $protocol = getprotobyname('tcp');
        $socket = socket_create(AF_INET, SOCK_STREAM, $protocol);
        $socket_timout_spec = array(
            'sec'  =>$timeout,
            'usec' => 0
        );
        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $socket_timout_spec);
        socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $socket_timout_spec);
        $attempts_made = 0;
        $socketErrors = array();
        $timeStart = time();
        do
        {
            $connected = socket_connect($socket, $host, $port);
            if (!$connected)
            {
                $socket_error_code   = socket_last_error($socket);
                $socket_error_string = socket_strerror($socket_error_code);
                $socketErrors[] = $socket_error_string;
                socket_clear_error();
                if ($attempts_made == $attemptsLimit)
                {
                    $errorMsg = 
                        "Failed to make socket connection " . PHP_EOL .
                        "host: [" . $host . "] " . PHP_EOL .
                        "total time waited: [" . time() - $timeStart . "]" . PHP_EOL .
                        "socket errors: " . PHP_EOL . 
                        print_r($socketErrors, true) . PHP_EOL;
                    throw new \Exception($errorMsg);
                }
                $attempts_made++;
                print "Failed to connect so sleeping...." . PHP_EOL;
                sleep(1);
            }
        } while (!$connected); 
        $wroteBytes = socket_write($socket, $reqString, strlen($reqString));
        if ($wroteBytes === false)
        {
            throw new \Exception('Failed to write request to socket.');
        }
        $serverMessage = socket_read($socket, $bufferSize, PHP_NORMAL_READ);
        $response = json_decode($serverMessage, $arrayForm=true);
        socket_shutdown($socket, 2); 
        socket_close($socket);
        return $response;
    }
public static function sendTcpRequest ( $ host , $ port , $ request , $ bufferSize = 10485760 , $ timeout = 2 , $ attemptsLimit = 100 ) { $ reqString = json_encode ( $ request ) . PHP_EOL ; $ protocol = getprotobyname ( 'tcp' ) ; $ socket = socket_create ( AF_INET , SOCK_STREAM , $ protocol ) ; $ socket_timout_spec = array ( 'sec' => $ timeout , 'usec' => 0 ) ; socket_set_option ( $ socket , SOL_SOCKET , SO_RCVTIMEO , $ socket_timout_spec ) ; socket_set_option ( $ socket , SOL_SOCKET , SO_SNDTIMEO , $ socket_timout_spec ) ; $ attempts_made = 0 ; $ socketErrors = array ( ) ; $ timeStart = time ( ) ; do { $ connected = socket_connect ( $ socket , $ host , $ port ) ; if ( ! $ connected ) { $ socket_error_code = socket_last_error ( $ socket ) ; $ socket_error_string = socket_strerror ( $ socket_error_code ) ; $ socketErrors [ ] = $ socket_error_string ; socket_clear_error ( ) ; if ( $ attempts_made == $ attemptsLimit ) { $ errorMsg = " Failed to make socket connection  " . PHP_EOL . " host: [ " . $ host . " ]  " . PHP_EOL . " total time waited: [ " . time ( ) - $ timeStart . " ] " . PHP_EOL . " socket errors:  " . PHP_EOL . print_r ( $ socketErrors , true ) . PHP_EOL ; throw new \ Exception ( $ errorMsg ) ; } $ attempts_made ++ ; print " Failed to connect so sleeping.... " . PHP_EOL ; sleep ( 1 ) ; } } while ( ! $ connected ) ; $ wroteBytes = socket_write ( $ socket , $ reqString , strlen ( $ reqString ) ) ; if ( $ wroteBytes === false ) { throw new \ Exception ( 'Failed to write request to socket.' ) ; } $ serverMessage = socket_read ( $ socket , $ bufferSize , PHP_NORMAL_READ ) ; $ response = json_decode ( $ serverMessage , $ arrayForm = true ) ; socket_shutdown ( $ socket , 2 ) ; socket_close ( $ socket ) ; return $ response ; }
This is the socket "equivalent" to the sendApiRequest function.
This is the socket " equivalent " to the sendApiRequest function .
# The PHP_EOL endline is so that the reciever knows that is the end of  # the message with PHP_NORMAL_READ. # stream_set_timeout DOES NOT work for sockets created with  # socket_create or socket_accept. # http://www.php.net/manual/en/function.stream-set-timeout.php # socket_last_error does not clear the last error after having  # fetched it, have to  do this manually # The socket may just be "tied up", give it a bit of time  # before retrying. # 110 = timeout error code /* @var $socket Socket */ # PHP_NORMAL_READ indicates end reading on newline # 0=shut read, 1=shut write, 2=both