2013-07-15 13 views
0

我一直在试图在PHP上使用curl的例子。但是,没有我尝试的作品。我想要做的只是使用PHP读取,写入,搜索有关混乱的数据。但是,对于初学者来说,这似乎不是一个简单的方法。如何使用PHP读取和写入cloudant

代码在这里:

//Get DB's 
$returned_content = get_data('https://**InstanceName**:**Password**@**InstanceName**.cloudant.com/_all_dbs'); 

function get_data($url) 
{ 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 

我得到的错误是:

{"error":"unauthorized","reason":"Name or password is incorrect"}   
+0

cloudant API密钥是什么错误? – Mark

+0

{“错误”:“未授权”,“原因”:“名称或密码不正确”} –

回答

0

如果您使用PHP与您Cloudant数据库进行交互,你可能要检查出SAG API库。这使事情变得相当容易。有关如何在其网站上使用此API的一些很好的示例。希望这可以帮助。

您可能还想查看IBM新的Bluemix环境,他们将Cloudant作为可用服务之一。 Cloudant和Bluemix之间的集成非常好。

http://www.saggingcouch.com/

http://www.bluemix.net

0

//这是我如何获得PHP

$username='yourusername'; 
$password='yourpassword'; 
$URL='https://yourusername.cloudant.com/_api/v2/api_keys'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$URL); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
//curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
//$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code 
$response=curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
//$httpCode = 303; // test a fail code 
if($httpCode == 201) { 
     $json = json_decode($response, true); 
      if($json['ok'] == 1) { 
       $cloudantkey = $json['key']; 
       $cloudantpassword = $json['password']; 
       echo "Obtained Cloud Api Keys.. <br />"; 
      } else { 
       echo("error $httpCode"); 
       die(); 
      } 
} else { 
    echo("error $httpCode"); 
    die(); 

} 
if(curl_error($ch)){ 
    echo("curl error"); 
    die(); 
} 
curl_close ($ch);