如何使用php发出post请求使用curl保持会话活动
我正在使用php抓取需要会话才能登录的WWW.Example.com。我已经使用php curl成功登录。 请检查此链接。现在在该链接中,它显示了如何使用相同的会话cookie发出第二个请求 我的问题是刮伤的时候如何使用php发出post请求使用curl保持会话活动,php,session,post,session-cookies,libcurl,Php,Session,Post,Session Cookies,Libcurl,我正在使用php抓取需要会话才能登录的WWW.Example.com。我已经使用php curl成功登录。 请检查此链接。现在在该链接中,它显示了如何使用相同的会话cookie发出第二个请求 我的问题是刮伤的时候 define("COOKIE_FILE", "cookie.txt"); // Login the user $ch = curl_init('http://api.example.com/login/joe/smith'); curl_setopt
define("COOKIE_FILE", "cookie.txt");
// Login the user
$ch = curl_init('http://api.example.com/login/joe/smith');
curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
echo curl_exec ($ch);
// Read the session saved in the cookie file
echo "<br/><br/>";
$file = fopen("cookie.txt", 'r');
echo fread($file, 100000000);
echo "<br/><br/>";
// Get the users details
$ch = curl_init('http://api.example.com/user');
curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
echo curl_exec ($ch);
当第二个请求是GET请求时,一切都运行得非常好。当涉及到POST请求时,网站返回“您的会话已过期,请重新登录”
如何解决这个问题?我尝试了几种解决方案,在谷歌上搜索了一下,什么都没用。最后决定在这里发帖。提前谢谢
define("COOKIE_FILE", "cookie.txt");
// Login the user
$ch = curl_init('http://api.example.com/login/joe/smith');
curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
echo curl_exec ($ch);
// Read the session saved in the cookie file
echo "<br/><br/>";
$file = fopen("cookie.txt", 'r');
echo fread($file, 100000000);
echo "<br/><br/>";
// Get the users details
$ch = curl_init('http://api.example.com/user');
curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=abc&password=xyz&domain=123');
echo curl_exec ($ch);
# Netscape HTTP Cookie File # https://curl.haxx.se/docs/http-cookies.html # This file was generated by libcurl! Edit at your own risk. www.example.com FALSE /unifyv3 FALSE 0 JSESSIONID 5366C3DB90DE7187A66E0BD19F595309
HTTP/1.1 200 OK Date: Fri, 05 Aug 2016 19:48:25 GMT Server: Apache/2.2.3 (CentOS) Set-Cookie: JSESSIONID=225D8E499DE23F0FDF454D73F0C05F25; Path=/unifyv3 Content-Length: 2850 Connection: close Content-Type: text/html;charset=UTF-8
Logout
Your session has expired
Click here to log in again
Redirecting to Login Page in secs.