Execute PHP script as a Windows service

Under Windows, scripts can be, that's endless run with PHP using a service implemented.

This has the advantage of, that is the memory consumption with the time to infinity, with endless script versions and a recovery and restart functionality can be implemented, to keep the service over long periods of time running.

Furthermore, the service receives from the operating system events, if e.g.. a shutdown is, to be able to stop in time itself and not corrupt data to produce cancellation in a non-atomic operation.

To create a Windows service, you need the win32service PHP library.

You can here them Download and in the php.ini embed:

extension=php_win32service.dll

Create service more…

PHP cURL verify a self-signed FTPS certificate

Unfortunately, you can with the PHP FTP extension no SSL certificates of FTPS servers to verify, to prevent man-in-the-middle attacks.

But it is possible through PHP-cURL the self-signed certificate to verify:

public function checkFTPSCertificate(): bool
{
    $ftp_certificate = 'path/to/cert.crt'

    $ftp_server = 'ftp://foo.de/';
    $ftp_user = 'user';
    $ftp_password = 'password';

    $ch = curl_init();

    // curl_setopt($ch, CURLOPT_VERBOSE, '1');
    curl_setopt($ch, CURLOPT_URL, $ftp_server);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $ftp_user . ':' . $ftp_password);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);


    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1'); // Überprüfung des Serverzertifikats
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '2');
    curl_setopt($ch, CURLOPT_CAINFO , $ftp_certificate);
    curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_ALL);
    curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);

    $result = curl_exec($ch);
    $error_no = curl_errno($ch);
    $error_msg = curl_error($ch);
    curl_close ($ch);
    return $error_no == 0 && empty($error_msg);
}

Working Gau Frette with file streams

To with Gau Frette (Symfony extension) to be able to perform also stream operations on files, can you get the little scrawny documented Stream wrapper use.

This is configured in the gaufrette.yaml:

knp_gaufrette:
    stream_wrapper: ~
    filesystems:
        backup1:
            adapter: backup

And can then be z.B. the following mass is a csv. Write file:

$stream = fopen('gaufrette://backup1/datei.csv', 'w+');

fputcsv($stream, [1,2,3]);

Dynamically configure symfony service ID using parameters

If you like in his Depnedency configuration of injection (services.yml) the service injections make dynamically configurable wants about the parameters.yml,

parameters:
    my_class: 'App\MyClass'

can you do this using the Symfony expression language component do:

composer require symfony/expression-language

Then you can define in the services.yml:

services:
    App\Command\MyCommand:
      arguments:
        - '@=service(parameter("my_class"))'

Windows composer update fatal error: Allowed memory size of 1610612736 bytes exhausted

My composer despite unlimited storage has the following error message:

 composer update
 Loading composer repositories with package information
 Updating dependencies (including require-dev)

 VirtualAlloc() failed: [0x00000008] Zur Verarbeitung dieses Befehls sind nicht gen
 VirtualAlloc() failed: [0x00000008] Zur Verarbeitung dieses Befehls sind nicht gen

 Fatal error: Out of memory (allocated 1260388352) (tried to allocate 201326600 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/RuleSet.php on line 83

If you can no longer perform a composer update under Windows, Although it has the memory limit to unlimited, then you have a problem:

The Windows Xammp is only as 32 bit system available and RAM can provide only 2 GB at the moment, a further increase is not possible.

The solution is to run composer with a 64-bit PHP on the console (This then has up to 4 GB memory).

  • Download PHP x 64 version
  • unzip it and creating a php.ini (the umbennnen php.ini-development)
  • increase memory limit in php.ini: memory_limit =. -1
  • run on the console:
"C:\php-7.2.12-nts-Win32-VC15-x64\php" C:\ProgramData\ComposerSetup\bin\composer.phar update

Yii2 versus symfony 4 – a framework comparison

I have long worked with Yii and the symfony framework and want to try the following to explain the differences between the two frameworks from programmers point of view.

Configuration of environments

When you create Web applications is typically at least 2 Environments: The development environment, on which you want to see errors and the live environment, where this should happen. To come test environment to run automated tests and perhaps a staging environment.

Symfony raises the very skillfully, with the .env files and own folders within the configuration folder.

At Yii must the programmers themselves lend a hand, to load the correct configuration, There are only from home 3 (dev, Prod, test) Vorgsehen environments, then it is confusing. more…

PHP encode string for extended ASCII 8 bit 255 Sign

To represent also umlauts in a bit, can you get the extended ASCII character set use with 255 Sign, It contains also umlauts like ä, ö, ü and ß.

In order to use this, can I use the following function:

/**
 * @param string $string
 * @return string
 */
protected function asciiEncodeString(string $string): string
{
    $sourceEncoding = mb_detect_encoding($string);
    $destinationEncoding = 'CP437'; // Extended ASCII - Codepage 437
    $string = iconv($sourceEncoding, $destinationEncoding, $string);
    return $string;
}

UDP with PHP send and receive sample messages

With PHP you can send very simple packets over UDP on your own computer. To test this, do you need a transmitter and a receiver. Communicate both via the local IP on port in the Biepsiel 20010. The samples must be run on the console.

Transmitter:

<?php
$address = '127.0.0.1';
$port = 20010;
$beat_period = 1;

$fp = stream_socket_client("udp://$address:$port", $errno, $errstr);
if (!$fp) {
 die("ERROR: $errno - $errstr");
}

while (true) {
 $message = sprintf(
 '%s send: %s'. PHP_EOL,
 date('c'),
 rand(0, 1000000)
 );
 fwrite($fp, $message);
 echo $message;

 sleep($beat_period);
}

Receiver:

<?php

$address = '127.0.0.1';
$port = 20010;

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, $address, $port);

if ($socket === false) {
 throw new \RuntimeException(
 sprintf(
 'could not connect to socket address %s on port %s. Error: %s %s',
 $address,
 $port,
 socket_last_error(),
 socket_strerror(socket_last_error())
 )
 );
}

while (true){
 echo socket_read ($socket, 1024);
}