Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 136.3MB ·虚拟内存 1372.6MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
在本教程中,我们将说明php CouchDb连接的示例。 PHP提供了简单的连接方法。我们只需要执行下面给出的Php脚本即可。
默认情况下,CouchDB在5984端口上执行。
//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;
}
}
?>
我们可以使用 http: //localhost: 5984/_utils 查看可用的数据库。

现在在本地主机服务器上执行Php文件。之后,再次访问CouchDB。

请参见,我们的脚本已创建数据库 lidihuo 。它还包含一个带有值的文档。

该文档称为ID 24.通过单击,我们可以看到存储在文档中的值。值显示如下:


使用Python与CouchDB连接。我们必须安装以下软件包。Python解释器CouchDB数据库python-couchdb驱动程序我们正在Linux(Ubuntu 16.04)中执行此示例。此示例包括 ...