当我执行下面的代码时,它工作正常。
$url = "https://api.mongolab.com/api/1/databases/$DB/collections/$COLLECTION?apiKey=$MONGOLAB_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
)
);
echo $response = curl_exec($ch);
$error = curl_error($ch);
echo $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code == 200)
{
$response1=json_decode($response);
echo("<pre>");print_r($response1);echo("</pre>");
}
else
{
echo "Fail";
var_dump($data);
var_dump($status);
}
上面运行良好,它将一条记录插入到 mongolab 数据库并返回对象 id,但是当执行下面的代码时,它返回空响应而没有任何错误。我检查了所有这些都是正确的,但为什么响应为空。
$url = "https://api.mongolab.com/api/1/databases/winebook-dev/collections/persons?fo=true&q={'email':'$email'}&apiKey=".MONGOLAB_API_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200)
{
$data1 = json_decode($data);
$len = count($data1);
if($len > 0 )
{
echo "This email already resistered.";
}
else
{
$url = "https://api.mongolab.com/api/1/databases/$DB/collections/$COLLECTION?apiKey=$MONGOLAB_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
)
);
$response = curl_exec($ch);
$error = curl_error($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code == 200)
{
$response1=json_decode($response);
echo("<pre>");print_r($response1);echo("</pre>");
}
else
{
echo "Fail";
var_dump($data);
var_dump($status);
}
}
}
如果有人知道解决方案,那么突然回答这个问题。
谢谢你。