Redis の WebAPI (PHP CGI)

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Nginx + php-fpm で動作を確認しました。
[Redis の WebAPI を作成](https://qiita.com/ekzemplaro/items/1746b31beef382023f2d)

## Arch Linux で環境の用意

```bash
sudo pacman -S php-redis
```

```text:/etc/php/conf.d/igbinary.ini
[igbinary]
extension=igbinary.so
```

```text:/etc/php/conf.d/redis.ini
extension=redis
```

/etc/php/conf.d/igbinary.ini
/etc/php/conf.d/redis.ini
を
/etc/php/php-fpm.d にコピー

## プログラム

```php:redis_read.php
<?php
// ---------------------------------------------------------------------
//	redis_read.php
//
//					Jan/15/2020
//
// ---------------------------------------------------------------------
if (isset ($_POST['key']))
	{
	$key = $_POST['key'];
	try
		{
		$redis = new Redis();
		$redis->connect('localhost', 6379);
		$json_str = $redis->get ($key);
		echo $json_str;
		}
	catch  (PDOException $e)
		{
		print('Error:'.$e->getMessage());
		}
	}
// ---------------------------------------------------------------------
?>
```

```php:redis_insert.php
<?php
// ---------------------------------------------------------------------
//	redis_insert.php
//
//					Jan/15/2020
//
// ---------------------------------------------------------------------
if (isset ($_POST['key']))
	{
	$key = $_POST['key'];
	$json_str = $_POST['value'];
	try
		{
		$redis = new Redis();
		$redis->connect('localhost', 6379);
		$redis->set ($key,$json_str);
		echo $json_str;
		}
	catch  (PDOException $e)
		{
		print('Error:'.$e->getMessage());
		}
	}
// ---------------------------------------------------------------------
?>
```

```php:redis_list.php
<?php
// ---------------------------------------------------------------------
//	redis_list.php
//
//					Jan/15/2020
//
// ---------------------------------------------------------------------
	try
		{
		$redis = new Redis();
		$redis->connect('localhost', 6379);
		$keys = $redis->keys ('*');
		$json_str = json_encode ($keys);
		echo $json_str;
		}
	catch  (PDOException $e)
		{
		print('Error:'.$e->getMessage());
		}
// ---------------------------------------------------------------------
?>
```

## サーバーの状況　(Arch Linux)

```text
$ sudo systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; preset: >
     Active: active (running) since Wed 2023-08-02 09:55:24 JST; 6min ago
   Main PID: 3680 (php-fpm)
     Status: "Processes active: 0, idle: 2, Requests: 4, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 37748)
     Memory: 9.5M
        CPU: 69ms
     CGroup: /system.slice/php-fpm.service
             ├─3680 "php-fpm: master process (/etc/php/php-fpm.conf)"
             ├─3682 "php-fpm: pool www"
             └─3683 "php-fpm: pool www"
```

## テストスクリプト

```bash:httpie_read.sh
URL="http://localhost/uchida/tmp/get_qiita/p0652/redis_read.php"
#
http --form POST $URL key=t1856
```

実行結果

```text
$ ./httpie_read.sh 
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Wed, 02 Aug 2023 01:06:02 GMT
Server: nginx/1.24.0
Transfer-Encoding: chunked
X-Powered-By: PHP/8.2.9

{
    "date_mod": "2001-5-12",
    "name": "鯖江",
    "population": 32914
}
```

```bash:http_list.sh
URL="http://localhost/uchida/tmp/get_qiita/p0652/redis_list.php"
#
http POST $URL
```

実行結果

```text
$ ./http_list.sh 
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/html; charset=UTF-8
Date: Wed, 02 Aug 2023 01:11:22 GMT
Server: nginx/1.24.0
Transfer-Encoding: chunked
X-Powered-By: PHP/8.2.9

[
    "t1852",
    "t1854",
    "t1856",
    "t1857",
    "t1858",
    "t1855",
    "t1853",
    "t1851",
    "t1859"
]
```