如何使用C ++ cURL(libcurl)管理简单的PHP会话 [英] How to manage a simple PHP session using C++ cURL (libcurl)

查看:476
本文介绍了如何使用C ++ cURL(libcurl)管理简单的PHP会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个使用libcurl与PHP脚本通信的C ++客户端。

I'm writing a C++ client which is using libcurl for communicating with a PHP script.

通信应该基于会话,因此第一个任务是登录并使PHP脚本设置一个会话。

The communication should be session based, and thus the first task is to login and make the PHP script set up a session.

我不习惯使用C ++或PHP的会话。我基本上知道它与cookies和通信会话ID。

I'm not used to working with sessions either from C++ or PHP. I basically know that it has to do with cookies and communicating session id.

我找不到任何示例curl主页,演示了一个简单的会话管理用例。

I can't find any example on the curl homepage which demonstrates a simple session management use case.

我假设它与curl中的一个或多个下列选项有关:

I'm assuming it has something to do with one or many of the following options in curl:

CURLOPT_COOKIE
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
CURLOPT_COOKIESESSION
CURLOPT_COOKIELIST

但是我不能真正看到来自CURLOPT_COOKIESESSION的文档的大图片。

But I can't really see the big picture just from the documentation of CURLOPT_COOKIESESSION for instance.

任何人都这样做

尊敬的

Robert

推荐答案

据我所知,CURL将自动为您处理会话cookie,只要您重新使用您的CURL句柄为每个请求会话:

As far as I understand it, CURL will handle session cookies automatically for you if you enable cookies, as long as you reuse your CURL handle for each request in the session:

CURL *Handle = curl_easy_init();

// Read cookies from a previous session, as stored in MyCookieFileName.
curl_easy_setopt( Handle, CURLOPT_COOKIEFILE, MyCookieFileName );
// Save cookies from *this* session in MyCookieFileName
curl_easy_setopt( Handle, CURLOPT_COOKIEJAR, MyCookieFileName );

curl_easy_setopt( Handle, CURLOPT_URL, MyLoginPageUrl );
assert( curl_easy_perform( Handle ) == CURLE_OK );

curl_easy_setopt( Handle, CURLOPT_URL, MyActionPageUrl );
assert( curl_easy_perform( Handle ) == CURLE_OK );

// The cookies are actually saved here.
curl_easy_cleanup( Handle );

我不肯定你需要同时设置COOKIEFILE和COOKIEJAR,那样。在任何情况下,您必须设置其中之一,以便在CURL中启用所有的Cookie。您可以执行以下简单的操作:

I'm not positive that you need to set both COOKIEFILE and COOKIEJAR, but the documentation makes it seem that way. In any case, you have to set one of the two in order to enable cookies at all in CURL. You can do something as simple as:

curl_easy_setopt( Handle, CURLOPT_COOKIEFILE, "" );

这将不会从磁盘读取任何Cookie,但会启用会话Cookie卷曲柄。

That won't read any cookies from disk, but it will enable session cookies for the duration of the curl handle.

这篇关于如何使用C ++ cURL(libcurl)管理简单的PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆