Redis的Web API(PHP CGI)

这是符合我们在这里确定的规范要求的服务器端程序。我已经在Nginx + php-fpm环境下进行了测试。并且创建了Redis的WebAPI。

在Arch Linux上准备环境。

sudo pacman -S php-redis
[igbinary]
extension=igbinary.so
extension=redis

将/etc/php/conf.d/igbinary.ini和/etc/php/conf.d/redis.ini复制到/etc/php/php-fpm.d。

程式

<?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
//
//					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
//
//					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"

测试脚本 jīă

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
}
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"
]
广告
将在 10 秒后关闭
bannerAds