-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcouchsimple.php
157 lines (125 loc) · 4.12 KB
/
couchsimple.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
require_once (dirname(__FILE__) . '/config.inc.php');
$couch = new CouchSimple($config['couchdb_options']);
//--------------------------------------------------------------------------------------------------
class CouchSimple
{
//----------------------------------------------------------------------------------------------
function __construct($options)
{
foreach($options AS $key => $value) {
$this->$key = $value;
}
}
//-----------------------------------------------------------------------------------
// Do HTTP HEAD to see if a document exists
function exists($id)
{
$ch = curl_init();
$url = $this->prefix . $this->host . ':' . $this->port . '/' . $this->database . '/' . urlencode($id);
echo $url . "\n";
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
if (isset($this->proxy))
{
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD");
// http://stackoverflow.com/a/770200
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//echo $response;
return ($http_code == 200);
}
//----------------------------------------------------------------------------------------------
function send($method, $url, $post_data = NULL)
{
global $config;
$ch = curl_init();
$url = $this->prefix . $this->host . ':' . $this->port . $url;
//echo $url;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/cacert.pem");
// Set HTTP headers
$headers = array();
$headers[] = 'Content-type: application/json'; // we are sending JSON
// Override Expect: 100-continue header (may cause problems with HTTP proxies
// http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/
$headers[] = 'Expect:';
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
if (isset($this->proxy))
{
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
if (!empty($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
if (!empty($post_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
break;
case 'DELETE':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
}
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//echo $response;
if (curl_errno ($ch) != 0 )
{
echo "CURL error: ", curl_errno ($ch), " ", curl_error($ch);
}
return $response;
}
// Add, update, or delete object. This method is handy if you are unsure of whether object
// already exists. If it does, we get the revision number and then update the record using PUT
function add_update_or_delete_document($obj, $id, $operation = 'add')
{
if ($operation == 'add')
{
// add (PUT as we have identifier)
$resp = $this->send("PUT", "/" . $this->database . "/" . urlencode($id), json_encode($obj));
$r = json_decode($resp);
if (isset($r->error))
{
if ($r->error == 'conflict')
{
// Document exists, try update instead
$operation = 'update';
}
}
}
switch ($operation)
{
case 'delete':
case 'update':
$resp = $this->send("GET", "/" . $this->database . "/" . urlencode($id));
$r = json_decode($resp);
$rev = $r->_rev;
if ($operation == 'delete')
{
$resp = $this->send("DELETE", "/" . $this->database . "/" . urlencode($id) . '?rev=' . $rev);
}
else
{
$obj->_rev = $rev;
$resp = $this->send("PUT", "/" . $this->database . "/" . urlencode($id), json_encode($obj));
}
var_dump($resp);
default:
break;
}
var_dump($resp);
}
}
?>