mongolab rest api返回null响应

时间:2013-04-04 06:56:14

标签: php curl mlab

当我执行下面的代码时,它可以正常工作。

    $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数据库并返回Object id,但是当执行下面的代码时,它返回null响应而没有任何错误。 我检查了所有事情是对的,但为什么响应为空。

$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);
        }
    }
}

如果有人知道解决方案,那么突然回答这个问题。

谢谢你。

0 个答案:

没有答案