【发布时间】:2016-08-10 10:45:53
【问题描述】:
我想为我的 Cloudant 数据库定义不同的搜索字符串。但是我的 curl 函数有问题。你看出什么不对了吗?
function get_results($url, $bookmark = NULL, $results = array()){
$username = 'name';
$password = 'pw';
$url = $bookmark !== NULL ? $url."&bookmark=$bookmark" : $url;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$r = curl_exec($ch);
curl_close($ch);
$response = json_decode($r);
if(count($response->rows) === 200){
foreach($response->rows as $row){
$results[] = $row->doc;
}
return get_results($url, $response->bookmark, $results);
} else {
foreach($response->rows as $row){
$results[] = $row->doc;
}
return $results;
}
}
我收到此警告/通知:
Notice: Trying to get property of non-object in C:\xampp\... on line 62
Notice: Trying to get property of non-object in C:\xampp\... on line 68
Warning: Invalid argument supplied for foreach() in C:\xampp\... on line 68
第 62 行是:if(count($response->rows) === 200){
第 68 行是:foreach($response->rows as $row){
谢谢!
【问题讨论】:
-
了解基本的调试。
-
您是否检查过 $response 的值以确保它包含您尝试访问的属性。这些错误告诉您您正在尝试访问不存在的属性。例如,$response->rows,它告诉你 $response 中不存在“rows”。设置 $response 的值后尝试 print_r($response)。
-
谢谢,你是对的。它是空的。在我的笔记本电脑上是相同的 PHP 版本和相同的 xamp 版本,它可以工作,但不能在电脑上 -.- 我不明白。