1

可能重复:
curl_exec() 总是返回 false

嗨,我是 php 新手,现在我被这件事困住了一段时间。所以我有两个拥有自己数据库的域。因此,如果用户尝试登录一个域并且在数据库中找不到他的凭据,则它会卷曲到其他域以查看他的凭据是否存在。所以匹配意味着 num_of_rows = 1,否则为 0。

在 curl 工作正常后,我可以卷曲所有内容,但我没有得到任何回报。这是我的代码:-

登录检查.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 返回值之后打印,但在此之前打印。而且也没有收到任何价值。有什么想法为什么?

4

1 回答 1

3

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

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

注意===严格相等测试的使用。这是区分真正的布尔 false 和自然类型转换为 false 的结果(空字符串0,等等)所必需的。

于 2012-11-29T19:01:17.113 回答