使用 PHP 和 CURL 从 Cloudant 获取所有文档

Getting all docs from Cloudant with PHP and CURL

我正在尝试使用 PHP 和 CURL 从我托管在 Cloudant 中的 CouchDB 中获取所有文档。

到目前为止我已经试过了,我得到了 200 状态,但控制台的 Response 列上什么也没有。

<?php

$url = "https://myuser.cloudant.com/mydb/_all_docs?include_docs=true";
$user = 'myuser';
$pass = 'mypass';
$ch = curl_init();   // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);

$info = curl_getinfo($ch);
return $output;
return json_decode($output,true);   
curl_close($ch);

?>

我不流利 PHP,我错过了什么?

我觉得上面的代码示例有些奇怪。我通常将凭据放在 URL 中,所以 https://user:pass@myuser.cloudant.com 和 URL 一样 - 然后您不需要以下两行 curl_setopt。我不太确定单独传递它们会如何工作。

你也return两次。如果您只想检查输出,请尝试使用 var_dump() 命令,看看它是否显示您期望的结果?