在本教程中, 我们将说明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 "srcmini". $couch->send("PUT", "/srcmini"); // Create a new document in the database. $couch->send("PUT", "/srcmini/24", '{"_id":"24", "name":"John"}'); // Fetching document $resp = $couch->send("GET", "/srcmini/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 / _utilsto查看可用的数据库。
3)执行PHP脚本
现在在你的本地主机服务器上执行Php文件。之后, 再次访问CouchDB。
看, 我们的脚本已经创建了一个数据库srcmini。它还包含带有值的文档。
该文档被称为ID24。单击该文档可以看到存储在文档中的值。值显示如下:
4)获取数据
srcmini
评论前必须登录!
注册