如何使用php / html连接json

时间:2017-02-04 07:53:37

标签: php html json

我的数据库作为json在cloudant上。我想创建可以访问数据库的前端(表单)。更新它。如何使用html / php&如何将html / php与我的数据库连接?我是这个领域的新人,请帮忙。您也可以建议教程

1 个答案:

答案 0 :(得分:1)

看来cloudant使用NoSQL(CouchDB)技术。 因此,在cloudant网站上,您有一个教程here

以下是CRUD的示例: 跑步: 安装PHP 5.3.3或更高版本 通过Composer安装Guzzle: $ composer install 设置vars user,pass和db php crud.php

<?php
require 'vendor/autoload.php';
use Guzzle\Http\Client;
$user = getenv('user');
$pass = getenv('pass');
$db = getenv('db');
echo "$user $pass $db\n";
$baseUrl = "https://$user:$pass@$user.cloudant.com/$db";
$client = new Client($baseUrl);
$client->setDefaultOption('exceptions', false);
$putDbRequest = $client->put('');
$putDbResponse = $putDbRequest->send();
$doc = array('name' => 'john', 'age' => 35);
$postDocRequest = $client->post('', array("Content-Type" => "application/json"), json_encode($doc));
$postDocResponse = $postDocRequest->send();
$rev1 = json_decode($postDocResponse->getBody())->{'rev'};
$id = json_decode($postDocResponse->getBody())->{'id'};
echo "The new document's id is $id and the first revision is $rev1.\n";
$doc['age'] = 36;
$doc['_rev'] = $rev1;
$putDocRequest = $client->put("$id", array(), json_encode($doc));
$putDocResponse = $putDocRequest->send();
echo $putDocRequest;
echo $putDocResponse;
$rev2 = json_decode($putDocResponse->getBody())->{'rev'};
echo "The second revision is $rev2.\n";
echo "Now we will delete the document. This is the response we got:\n";
$deleteRequest = $client->delete("$id?rev=$rev2");
$deleteResponse = $deleteRequest->send();
echo $deleteResponse->getBody();