繁体   English   中英

如何在PHP中使用curl保持会话信息?

[英]How to keep session informations using curl in php?

仅当我第一次登录时,才可以访问虚拟页面www.randomDomain.com/onlyForRegisteredUsers 所以我这样做:

...
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->currentUseragent;
curl_setopt($this->curl, CURLOPT_PROXY, $this->currentIP);
curl_setopt($this->curl, CURLOPT_URL, $this->currentUrl); //www.randomDomain.com/login/
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->currentData);
    //with login RandomUserName and pass *******
$page = curl_exec($this->curl);

在$ page中成功获得了如下结果: ...<p>Welcome back, RandomUserName!</p>

如何保留有关我已经登录的会话的信息,然后从www.randomDomain.com/onlyForRegisteredUsers读取内容?

你可以这样做 :

$cookieFile = 'cookie.txt';
curl_setopt($this->curl, CURLOPT_COOKIESESSION, true);
curl_setopt($this->curl, CURLOPT_COOKIEJAR, realpath($cookieFile));
curl_setopt($this->curl, CURLOPT_COOKIEFILE, realpath($cookieFile));

注意:cookie文件必须存在,如果不存在,可以使用以下命令创建该文件:

if (!file_exists(realpath($cookieFile)))
{
    touch($cookieFile);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM