CouchDB教程

PHP CouchDB连接

在本教程中,我们将说明php CouchDb连接的示例。 PHP提供了简单的连接方法。我们只需要执行下面给出的Php脚本即可。
默认情况下,CouchDB在5984端口上执行。
1)创建一个Php文件
//index.php
<?php
$options['host'] = "localhost"; 
$options['port'] = 5984;
// Creating connection
$couch = new CouchSimple($options); 
$couch->send("GET", "/"); 
// Create a new database "lidihuo".
$couch->send("PUT", "/lidihuo"); 
// Create a new document in the database.
$couch->send("PUT", "/lidihuo/24", '{"_id":"24","name":"John"}'); 
// Fetching document
$resp = $couch->send("GET", "/lidihuo/24"); 
echo $resp; 
class CouchSimple {
function CouchSimple($options) {
foreach($options AS $key => $value) {
$this->$key = $value;
}
} 
function send($method, $url, $post_data = null) {
$s = fsockopen($this->host, $this->port, $errno, $errstr); 
if(!$s) {
echo "$errno: $errstr\n"; 
return false;
} 
$request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n"; 
if ($this->user) {
$request .= "Authorization: Basic ".base64_encode("$this->user:$this->pass")."\r\n"; 
}
if($post_data) {
$request .= "Content-Length: ".strlen($post_data)."\r\n\r\n"; 
$request .= "$post_data\r\n";
} 
else {
$request .= "\r\n";
}
fwrite($s, $request); 
$response = ""; 
while(!feof($s)) {
$response .= fgets($s);
}
list($this->headers, $this->body) = explode("\r\n\r\n", $response); 
return $this->body;
}
}
?>
    
2)访问CouchDB
我们可以使用 http: //localhost: 5984/_utils 查看可用的数据库。
CouchDB Phpouchdb Connectivity 1
3)执行Php脚本
现在在本地主机服务器上执行Php文件。之后,再次访问CouchDB。
CouchDB Php couchdb连接2
请参见,我们的脚本已创建数据库 lidihuo 。它还包含一个带有值的文档。
CouchDB Phpouchdb连接3
该文档称为ID 24.通过单击,我们可以看到存储在文档中的值。值显示如下:
CouchDB Phpouchdb连接性4
4)提取数据
CouchDB Php长沙发连接5
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4