php 如何在php中的cURL中维护会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2424714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:25:13  来源:igfitidea点击:

how to maintain session in cURL in php?

phpcurl

提问by newbie programmer

how can we maintain session in cURL?

我们如何在 cURL 中维护会话?

i'am having a code the sends login details of a site and logs in successfully i need to get the session maintained at the site to continue.

我有一个代码,可以发送站点的登录详细信息并成功登录,我需要在站点上维护会话才能继续。

here is my code that used to login to the site using cURL

这是我用于使用 cURL 登录站点的代码

  <?php  
        $socket = curl_init();
        curl_setopt($socket, CURLOPT_URL, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_REFERER, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_POST, true);
    curl_setopt($socket, CURLOPT_USERAGENT, $agent);
    curl_setopt($socket, CURLOPT_POSTFIELDS, "form_logusername=XXXXX&form_logpassword=XXXXX");
    curl_setopt($socket, CURLOPT_COOKIESESSION, true);
    curl_setopt($socket, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($socket, CURLOPT_COOKIEFILE, "cookies.txt");
    $data = curl_exec($socket);
    curl_close($socket); 
   ?>

回答by Jeffrey Nicholson Carré

Here is the best way i found to do this link:

这是我发现执行此链接的最佳方法:

the text bellow is a 'remixed' version of the content of the blogpost:

下面的文字是博文内容的“重新混合”版本:

 $useragent = $_SERVER['HTTP_USER_AGENT'];
 $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

session_write_close();

$ch = curl_init();
$ch = curl_init($rssFeedLink); 
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );

$response = curl_exec($ch); 
curl_close($ch); 

What does session_write_close()do? It, ends the current session and store session data. Apparently, PHP does not like when multiple scripts play around with the session, so, it locks it. Putting session_write_close makes sure that your current session is stored so you can retrieve it and use it.

有什么作用session_write_close()?它结束当前会话并存储会话数据。显然,PHP 不喜欢多个脚本在会话中运行,因此,它会锁定它。放置 session_write_close 确保您的当前会话被存储,以便您可以检索和使用它。

if you don't use session_write_close()a new session id will be generated instead of using the current session id.

如果您不使用session_write_close()新的会话 id,则将生成而不是使用当前的会话 id。

Also PHPSESSIDshould be replaced by the name of the session variable. According to OWSAP recommandationsit should be something more general like anId.

此外,PHPSESSID应替换为会话变量的名称。根据OWSAP 的建议,它应该是更通用的东西,比如anId

Sometimes you will need to send a user agent with the post so i included the CURLOPT_USERAGENTparameter.

有时您需要发送带有帖子的用户代理,因此我包含了该CURLOPT_USERAGENT参数。

回答by raveren

This is how you do CURL with sessions

这就是你如何用 session 做 CURL

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

I've seen this on ImpressPages

我在 ImpressPages 上看到过这个

回答by wimvds

Since you're referring to a cookies.txt file without any reference to a folder my first guess would be that you're trying to write to a file in a folder that isn't writable. So first check if you actually find a cookies.txt file and if it contains the session cookie(s) you would expect.

由于您指的是 cookies.txt 文件而没有对文件夹的任何引用,我的第一个猜测是您正在尝试写入不可写文件夹中的文件。因此,首先检查您是否确实找到了 cookies.txt 文件,以及它是否包含您期望的会话 cookie。

回答by Sabeen Malik

I would probably move that code into a function. After successful login, you now have the session associated with the cookie you have in your cookie.txt file. On subsequent requests, just keep using that cookie file and you should have a valid session on that site.

我可能会将该代码移动到一个函数中。成功登录后,您现在拥有与 cookie.txt 文件中的 cookie 相关联的会话。在后续请求中,只需继续使用该 cookie 文件,您就应该在该站点上有一个有效的会话。