没有从curl php获得返回值

时间:2012-11-29 18:57:10

标签: php curl

  

可能重复:
  curl_exec() always returns false

嗨,我是php新手,现在我一直坚持这件事。所以我有两个域名和自己的数据库。因此,如果用户尝试登录一个域并且在数据库中找不到他的凭据,则会向其他域卷曲以查看其凭据是否存在。所以匹配意味着num_of_rows = 1,否则为0.

卷曲工作正常后,我能卷曲一切,但我不会得到任何回报。这是我的代码: -

logincheck.php:

<?php
    session_start();
if(isset($_POST['Email'], $_POST['Password'])){
    $email = $_POST["Email"];   
    $password = $_POST["Password"];

        print "email received $email";
        print "<br>password received $password";

    $db_username="root";
    $db_password="root";
    $database="photobook";
    $localhost = "mysql";

    $con = mysql_connect($localhost,$db_username,$db_password);
    mysql_select_db($database,$con) or die( "Unable to select database");

    $query = "select * from photobook.users where email ='$email' and password ='$password';" ;

    $result = mysql_query($query);

    $num=mysql_num_rows($result);

    if($num == 1){
        while($row = mysql_fetch_array($result))
             {
                $_SESSION['email'] = $row['email'];

                $_SESSION['username'] = $row['username'];   
             }

        header("location: home.php");

    }

    else{

        include "Protocol.php";

                print "<br>email after protocol file include is $email";
                print "<br>password after protocol file include is $password";

        $obj=new Protocol();

        $val = $obj->loginCheck($email,$password);

                **print "<br>value of val received is $val";**

        if($val == 0){
            session_destroy();
                        print "<br>user does not exist";
            header("location: login.php");
        }
        else{
            $_SESSION['email'] = $val[0];
            $_SESSION['username'] = $val[1];
            print "<br>user exists";
            header("location: home.php");

        }   
    }

    mysql_close($con);

    }
?>
第一个域上的

Protocol.php: -

<?php

    class Protocol{   
        function loginCheck($p_email,$p_password){

            $sid = 2;

            $msg="sid=$sid&email=$p_email&password=$p_password";

            print "<br>parameter string sent $msg";

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,"http://www.snapreveal.com/PhotoBook/src/Protocol.php");
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            $p_result = curl_exec($ch);
            curl_close($ch);
            if ($p_result === FALSE) {
                  die("Curl error: " . curl_error($ch));
            }
            return $p_result;

        }

    } //end of protocol class
?>

第二个域上的Protocol.php: -

<?php

    class Protocol{

        function loginCheckResponse($p_email,$p_password){

            print "<br>inside second domain loginCheckResponse";
            print "<br>email inside second domain loginCheckResponse $p_email";
            print "<br>password inside second domain loginCheckResponse $p_password";

            $db_username="root";
            $db_password="root";
            $database="photobook";
            $localhost = "mysql";

            $con = mysql_connect($localhost,$db_username,$db_password);
            mysql_select_db($database,$con) or die( "Unable to select database");

            $query = "select * from photobook.users where email ='$p_email' and password ='$p_password';" ;

            $result = mysql_query($query);

            $p_num=mysql_num_rows($result);

            print "<br>num found inside teja loginCheckResponse $p_num";

            mysql_close($con);

            if($p_num == 1){
              while($row = mysql_fetch_array($result))
             {

                                $u_email = $row['email'];

                                $u_username = $row['username'];

                                $res = array($u_email,$u_username);
                                print "<br>inside second domain logincheck, after match values in res array are, email $res[0],username $res[1]";
                                print "<br>value in res<br>";
                                print $res;
                                return $res;
             }
            }
            else{
              return 0;
            }

        }

    } //end of protocol class

    $pobj=new Protocol();

    $sid = $_POST['sid'];

    $p_email = $_POST['email'];

    $p_password = $_POST['password'];

    if($_POST['sid'] == 2 ){
        $pobj->loginCheckResponse($p_email,$p_password);

        }
?>

印刷品的输出:

email received max@gmail.com
password received max
email after protocol file include is max@gmail.com
password after protocol file include is max
parameter string sent sid=2&email=max@gmail.com&password=max
value of val received is 
inside second domain loginCheckResponse
email inside second domain loginCheckResponse max@gmail.com
password inside second domain loginCheckResponse max
num found inside second domain loginCheckResponse 1
inside second domain logincheck, after match values in res array are, email max@gmail.com,username Max
value in res
Array
user does not exist

所以“收到的val的值是”这条线应该在curl返回值之后打印,但它在此之前打印。并且没有收到任何价值。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

curl在失败时返回布尔值FALSE,它将打印为空字符串。在处理远程服务时,您永远不应该取得成功,并始终检查错误:

    $p_result = curl_exec($ch);
    if ($p_result === FALSE) {
        die("Curl error: " . curl_error($ch));
    }
    curl_close($ch);

注意使用===严格相等测试。这是必要的,以区分真正的布尔值假,以及自然地将其指定为假(空字符串,0等...)的结果。