2013-07-15 56 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);