0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redis の WebAPI (PHP CGI)

Last updated at Posted at 2020-01-15

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Nginx + php-fpm で動作を確認しました。
Redis の WebAPI を作成

Arch Linux で環境の用意

sudo pacman -S php-redis
/etc/php/conf.d/igbinary.ini
[igbinary]
extension=igbinary.so
/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 にコピー

プログラム

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());
		}
	}
// ---------------------------------------------------------------------
?>
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());
		}
	}
// ---------------------------------------------------------------------
?>
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)

$ 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"

テストスクリプト

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

実行結果

$ ./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
}
http_list.sh
URL="http://localhost/uchida/tmp/get_qiita/p0652/redis_list.php"
#
http POST $URL

実行結果

$ ./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"
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?