如何在php的cURL中维护会话? [英] how to maintain session in cURL in php?
本文介绍了如何在php的cURL中维护会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何在 cURL 中维护会话?
how can we maintain session in 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.
这是我用来使用 cURL 登录网站的代码
here is my code that used to login to the site using 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);
?>
推荐答案
这就是你如何用 session 做 CURL
This is how you do CURL with sessions
//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);
}
我在 ImpressPages 上看到过这个
I've seen this on ImpressPages
这篇关于如何在php的cURL中维护会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文