0

I have been trying to use the Curl examples on cloundant using PHP. However, nothing I try works. All I want to do is simply read, write, search data on cloundant using PHP. However, there doesn't seem to be a simple way for a beginning developer to do this.

Code here:

//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;
}   

The error I get is:

{"error":"unauthorized","reason":"Name or password is incorrect"}           
nhahtdh
  • 55,989
  • 15
  • 126
  • 162

4 Answers4

1

According to How do I make a request using HTTP basic authentication with PHP curl?, you need to set the basic auth credentials outside the URL. Adapting their example to your variable names:

curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

Community
  • 1
  • 1
Mike Rhodes
  • 1,816
  • 12
  • 15
0

If your using PHP to interact with your Cloudant database you may want to check out the SAG api library. This makes things fairly easy to do. There are some great examples on how to use this API on their site. Hope this helps.

You may also want to check out IBM's new Bluemix environment where they have Cloudant as one of the available services. The integration between Cloudant and Bluemix is excellent.

http://www.saggingcouch.com/

http://www.bluemix.net

Dave Krier
  • 55
  • 6
0

I have used Node.js & NodeExpress with Cloudant. You could do similar stuff for PHP. See if these 2 posts help

A Cloud Medley with IBM Bluemix, Cloudant & Node.js Rock N'Roll with Bluemix, Cloudant & NodeExpress

user229044
  • 232,980
  • 40
  • 330
  • 338
Tinniam V. Ganesh
  • 1,979
  • 6
  • 26
  • 51
0

// this is how I get cloudant api keys in 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);
Simon Fearby
  • 177
  • 1
  • 6