使用Curl和PHP保持会话活动 [英] Keeping session alive with Curl and PHP

查看:174
本文介绍了使用Curl和PHP保持会话活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试连接到api,验证用户身份,然后查看用户详细信息。这是通过首先访问

  http://api.example.com/login/<username> /< password> 

登录,然后按以下方式查看用户详细信息:

  http://api.example.com/user/ 

这一切都在网络浏览器中工作。但是一旦我尝试使用Curl,登录工作正常,但是当尝试查看用户详细信息,我回来一个401,未经授权的错误。我相信这是因为Curl没有正确保存会话cookie?有人可以指出为什么它不工作,如何解决它?我试过搜索堆栈交换,但没有一个解决方案,我试过为我的情况。我用来卷曲端点的代码如下所示。感谢!

  define(COOKIE_FILE,cookie.txt); 

//登录用户
$ 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);

//读取保存在cookie文件中的会话
echo< br />< br />;
$ file = fopen(cookie.txt,'r');
echo fread($ file,100000000);
echo< br />< br />;

//获取用户详细信息
$ ch = curl_init('http://api.example.com/user');
curl_setopt($ ch,CURLOPT_COOKIEJAR,COOKIE_FILE);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_HEADER,true);
echo curl_exec($ ch);

此代码将输出:

  HTTP / 1.1 200 OK Date:Mon,22 Oct 2012 21:23:57 GMT服务器:LiteSpeed连接:关闭X-Powered-By:PHP / 5.3.14设置Cookie:cfapi = f481129c9616b8f69cc36afe16466545 ; path = / Expires:Thu,19 Nov 1981 08:52:00 GMT Cache-Control:no-store,no-cache,必须重新生效,post-check = 0,pre-check = 0 Pragma:类型:application / json X-Powered-By:CFWAPI 0.1a Content-Length:46 {status:200,msg:Successfully Logged In。} 

#Netscape HTTP Cookie File #http://curl.haxx.se/rfc/cookie_spec.html#此文件由libcurl生成!编辑自行承担风险。 api.example.com FALSE / FALSE 0 cfapi 94f63b07ccf7e34358c1c922341c020f

HTTP / 1.1 401未授权日期:星期一,2012年10月22日21:23:57 GMT服务器:LiteSpeed连接:close X-Powered-By:PHP /5.3.14 Set-Cookie:cfapi = a8eb015a7c423dde95aa01579c4729a4; path = / Expires:Thu,19 Nov 1981 08:52:00 GMT Cache-Control:no-store,no-cache,必须重新生效,post-check = 0,pre-check = 0 Pragma:类型:application / json X-Powered-By:CFWAPI 0.1a Content-Length:49 {status:401,msg:您需要先登录!}

解决方案

您还需要设置选项 CURLOPT_COOKIEFILE



手册将此描述为


包含cookie的文件名称数据。 cookie文件可以是
以Netscape格式,或者只是简单的HTTP风格的头转储到
文件中。如果名称是空字符串,则不会加载Cookie,但仍会启用
Cookie处理。


使用cookie jar结束时,请求完成时保存cookie,但由于未提供 CURLOPT_COOKIEFILE ,cURL不会在后续请求中发送任何已保存的Cookie。


I'm trying to connect to an api, authentice a user and then view the user details. This is accomplished by first accessing the login endpoint at

http://api.example.com/login/<username>/<password>

to login and then the following to view user details:

http://api.example.com/user/

This all works in a web browser. However once I try to use Curl, the login works fine, but when attempting to view user details, I get back a 401, unauthorized error. I believe this is because Curl isn't saving the session cookies properly? Can someone point out why it isn't working and how to fix it? I've tried searching stack exchange, however none of the solutions I've tried have worked for my situation. The code I'm using to curl the endpoints is shown below. Thanks!

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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
echo curl_exec ($ch);

This code will output:

HTTP/1.1 200 OK Date: Mon, 22 Oct 2012 21:23:57 GMT Server: LiteSpeed Connection: close X-Powered-By: PHP/5.3.14 Set-Cookie: cfapi=f481129c9616b8f69cc36afe16466545; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Type: application/json X-Powered-By: CFWAPI 0.1a Content-Length: 46 {"status":200,"msg":"Successfully Logged In."}

# Netscape HTTP Cookie File # http://curl.haxx.se/rfc/cookie_spec.html # This file was generated by libcurl! Edit at your own risk. api.example.com FALSE   /   FALSE   0   cfapi 94f63b07ccf7e34358c1c922341c020f 

HTTP/1.1 401 Unauthorized Date: Mon, 22 Oct 2012 21:23:57 GMT Server: LiteSpeed Connection: close X-Powered-By: PHP/5.3.14 Set-Cookie: cfapi=a8eb015a7c423dde95aa01579c4729a4; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Type: application/json X-Powered-By: CFWAPI 0.1a Content-Length: 49 {"status":401, "msg":"You need to login first!"}

解决方案

You also need to set the option CURLOPT_COOKIEFILE.

The manual describes this as

The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.

Since you are using the cookie jar you end up saving the cookies when the requests finish, but since the CURLOPT_COOKIEFILE is not given, cURL isn't sending any of the saved cookies on subsequent requests.

这篇关于使用Curl和PHP保持会话活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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