PHP cUrl Handle 资源替换为整数

PHP cUrl Handle resource is replaced by an integer

我正在使用 PHP curl 向需要登录的第 3 方服务器发送一系列请求,然后为该登录保留会话 cookie。

所以我将 curl 操作包装到这个 class:

class SoapCli {
    private $ch;
    private $id;
    private $rc;

    function __construct() {
        $this->rc=0;
        $this->id=bin2hex(random_bytes(8));
        $this->ch = curl_init();
        $time=microtime(true);
        error_log(PHP_EOL.PHP_EOL."Instance id $this->id created ($time): $this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
        curl_setopt($this->ch, CURLOPT_AUTOREFERER,1);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, "");
        curl_setopt($this->ch, CURLOPT_ENCODING, "");
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
    }

    function Request(string $method, string $url, array $headers = array(), $postdata = "", $referer = null) {
        $resp = new stdClass();
        $resp->id = $this->id;
        $this->rc++;
        $time=microtime(true);
        error_log("Instance id $this->id before request $this->rc ($time): $this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
        try {
            curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($this->ch, CURLOPT_URL, $url);
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
            if (isset($referer)) curl_setopt($this->ch, CURLOPT_REFERER, $referer);
            if (preg_match("/^POST$/i",$method)===1) curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);
            $resp->body = curl_exec($this->ch);
            $resp->err_message = curl_error($this->ch);
            $resp->err_number = curl_errno($this->ch);
            $resp->info = curl_getinfo($this->ch);
        }
        catch (Exception $exception) {
            $resp->err_message = $exception->getMessage();
            $resp->err_number = $exception->getCode();
            $resp->info = $exception->getTrace();
        }
        $time=microtime(true);
        error_log("Instance id $this->id before request $this->rc ($time): $this->ch = ".print_r($this->ch,true).PHP_EOL,3,"log.txt");
        return $resp;
    }
}

但是,在第 3 次请求之后,存储 curl 句柄资源的受保护变量的内容被 0(整数) 的值替换,我真的无法理解出为什么。我只能收集这个日志:

Instance id 1cb893bc5b7369bd created (1547852391.7976): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 1 (1547852391.8025): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 1 (1547852392.0723): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 2 (1547852392.0778): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 2 (1547852392.357): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 3 (1547852392.3616): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 3 (1547852392.6225): $this->ch = Resource id #3
Instance id 1cb893bc5b7369bd before request 4 (1547852393.0264): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 4 (1547852393.0758): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 5 (1547852394.8992): $this->ch = 0
Instance id 1cb893bc5b7369bd before request 5 (1547852394.9461): $this->ch = 0

编辑: 这是使用 class SoapCli:

的代码
// index.php

$postdata = filter_input_array(INPUT_POST);
if ($_SESSION["logged_in"]===true) {
    echo file_get_contents("main.html");
} else if (isset($postdata) && isset($postdata["action"])) {
    $action = $postdata["action"];
    if ($action==="Login" && isset($postdata["usrcpf"]) && isset($postdata["usrpwd"])) {
        $username=$postdata["username"];
        $password=$postdata["password"];
        $sc=new SoapCli();   //instantiated here
        $_SESSION["sc"]=$sc;
        $login_response = $sc->Request(
            "GET",
            BASEURL."/login",
            array(
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
                "Connection: keep-alive",
                "Upgrade-Insecure-Requests: 1",
                "Cache-Control: max-age=0"
                )
            );
        if ($login_response->err_number) {
            echo file_get_contents("login_server_error.html");
        } else {
            $dom = new DOMDocument;
            $dom->loadHTML($login_response->body);
            $xdom  = new DOMXPath($dom);
            $csrf_token_nodes = $xdom->query("//input[@name='_csrf_token']/@value");
            if ($csrf_token_nodes->length<1) {
                echo file_get_contents("login_server_error.html");
            } else {
                $csrf_token = $csrf_token_nodes->item(0)->textContent;
                $postdata = "_csrf_token=$csrf_token&_username=$username&_password=$password&_submit=Login";
                $login_check_response = $sc->Request(
                    "POST",
                    BASEURL."/login_check",
                    array(
                        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                        "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
                        "Content-Type: application/x-www-form-urlencoded",
                        "Connection: keep-alive",
                        "Upgrade-Insecure-Requests: 1"
                    ),
                    $postdata,
                    BASEURL."/login"
                    );
                if ($login_check_response->err_number) {
                    echo file_get_contents("login_server_error.html");
                } elseif (strpos($login_check_response->body, "api.js")) {
                    echo file_get_contents("login_auth_error.html");
                } else {
                    $route_userinfo = $sc->Request(
                        "POST",
                        BASEURL."/route",
                        array(
                             "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
                             "Accept: */*",
                             "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
                             "Content-Type: application/json",
                             "X-Requested-With: XMLHttpRequest",
                             "Connection: keep-alive",
                             "Upgrade-Insecure-Requests: 1",
                         ),
                        USERINFO_JSON,
                        BASEURL."/"
                        );
                    if ($route_userinfo->err_number) {
                        echo file_get_contents("login_server_error.html");
                    } else {
                        $_SESSION["logged_in"]=true;
                        $_SESSION["user_info"]=json_decode($route_userinfo->body);
                        header("Location: ".$_SERVER["PHP_SELF"], true, 303);
                    }
                }
            }
        }
    } else {
        http_response_code(400);
    }
} else {
    echo file_get_contents("login.html");
}

// ajax.php (called by JS in main.html, which is loaded after login)

if ($_SESSION["logged_in"]===true) {
    $postdata = filter_input_array(INPUT_POST);
    if (isset($postdata)) {
        if (isset($postdata["content"])) {
            if ($postdata["content"]==="tasks") {
                $sc=$_SESSION["sc"];
                $route_tasks = $sc->Request(
                    "POST",
                    BASEURL."/route",
                    array(
                         "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0",
                         "Accept: */*",
                         "Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
                         "Content-Type: application/json",
                         "X-Requested-With: XMLHttpRequest",
                         "Connection: keep-alive",
                         "Upgrade-Insecure-Requests: 1",
                     ),
                    TAKS_JSON,
                    BASEURL."/"
                    );
                if ($route_tasks->err_number) {
                    echo file_get_contents("ajax_server_error.html");
                } else {
                    $tarefas=json_decode($route_tasks->body);
                    if (isset($tarefas) && is_array($tarefas->records)) {
                        foreach($tarefas->records as $i=>$tarefa){
                            echo "<p>".$tarefa->especieTarefa->nome."</p>";
                        }
                    } else {
                        http_response_code(500);
                    }
                }
            }
        } else {
            http_response_code(400);
        }
    } else {
        http_response_code(400);
    }
} else {
    http_response_code(403);
}

由于无法从 class 访问变量 SoapCli::ch,我真的看不出如何在没有声明的情况下更改其内容。我也找不到任何有关会破坏句柄的 http request/response 的信息。

附加信息

不管它是什么,它与请求无关,因为我试图重复请求#3,它是有效的并且收到了有效的响应,但由于句柄已经消失,它的重复失败了。

此外,我在 PHP 中尝试实现的功能已经由功能齐全的 .NET 桌面 (winforms) 应用程序完成,因此它不会因为外部原因而无法完成。我只是想用 PHP curl 做我用 System.Net.HttpWebRequest 做的事情,然后偶然发现了这个 post.

中描述的问题

如何才能在需要时保留句柄?

我在 IIS Express/Windows 10.

上使用 PHP 7.2

简短的回答是:当您试图在 ajax.php

中使用句柄时,该句柄不存在

ajax.php 中,查看以下行:

                $sc=$_SESSION["sc"];

然后你打电话:

                $route_tasks = $sc->Request(
                   ...
                    );

所以你在 index.php 中设置了你的 class 并且在那里进行的所有 3 个调用都成功了,然后你将一个对象写入 $_SESSION["sc"] 变量并且显然对象被编码并且由 php 的会话处理程序正确解码,这就是为什么您在检索对象后仍然可以在 ajax.php 中调用方法 Request 的原因。

虽然您确实在 ajax.php 中使用了一个对象,但它与 index.php 创建的对象实例不同,因为该实例属于 index.php 线程curl 句柄;从 index.php 调用 ajax.php 将创建一个不同的线程来处理它,并且还需要一个新的 curl 句柄。

$sc=$_SESSION["sc"]; 更改为 $sc=new SoapCli();,以便可以在使用前创建 curl 句柄。

我发布这个答案只是为了展示我如何解决@Solrac 在他的答案中描述和解释的问题(这是正确的,我会接受):

class SoapCli {
    private $ch;
    private $cookiepot;

    function __construct() {
        $this->cookiepot=tempnam(sys_get_temp_dir(),"CookieJar");
        $this->reconstruct();
    }

    function reconstruct() {
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 300);
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookiepot);
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookiepot);
        curl_setopt($this->ch, CURLOPT_ENCODING, "");
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->ch, CURLOPT_HEADER, true);
        curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($this->ch, CURLOPT_MAXREDIRS, 32);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_VERBOSE, true);
    }

    function Request(string $method, string $url, array $headers = array(), $postdata = "", $referer = "") {
        if (!is_resource($this->ch)) {
            $this->reconstruct();
        }
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($this->ch, CURLOPT_REFERER, $referer);
        if (preg_match("/^POST$/i",$method)===1) curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);
        $response=curl_exec($this->ch);
        list($headers,$body)=preg_split("/\r\n\r\n(?!HTTP)/", $response, 2);
        $resp_obj = (object) array(
            "body"=>$body,
            "headers"=>$headers,
            "err_number"=>curl_errno($this->ch),
            "err_message"=>curl_error($this->ch),
            "info"=>curl_getinfo($this->ch)
        );
        return $resp_obj;
    }

    function log(string $text) {
        file_put_contents($this->id."log.txt",$text.PHP_EOL,FILE_APPEND|FILE_TEXT|LOCK_EX);
    }
}