【发布时间】:2015-02-21 14:43:15
【问题描述】:
我正在尝试向我的 laravel 服务器发出 curl 请求,在该请求中,我必须检查我的 laravel 应用程序的用户是否已登录。我使用此代码:
$transferAmount = 200;
//set POST variables
$url = URL::route('post-spend-partner');
$fields = array(
'transferAmount' => urlencode($transferAmount),
'cancelUrl' => urlencode(URL::route('get-return-page-example')),
'returnUrl' => urlencode(URL::route('get-return-page-example')),
);
// New Connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
在请求的 url 中,我只是检查我是否已登录,但它总是返回 false:
public function postSpendPartner() {
echo "Authenticated? " . (Auth::check() ? 'Yes' : 'No');
}
我确定我已经登录,如果我用 Ajax 尝试完全相同的东西,它完全可以工作!
有谁知道我可以尝试什么来解决这个问题?
最好的问候!
面料
【问题讨论】:
-
Laravel 使用 cookie 来识别会话,您的 curl 请求不会发送会话 cookie,因此无法匹配会话。
-
我尝试过这样的事情: // 新的 Cookie 文件 $cookieFile = "myCookiefile.txt"; if(!file_exists($cookieFile)) { $fh = fopen($cookieFile, "w"); fwrite($fh, ""); fclose($fh); } curl_setopt($ch, CURLOPT_COOKIE, 'laravel_session=' . $_COOKIE['laravel_session']); curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieFile); curl_setopt($ch, CURLOPT_COOKIESESSION, true);但这不起作用,我可以尝试什么?
-
顺便说一句,为什么它可以在 ajax 而不是 curl 中工作?
-
@Bogdan 你能告诉我如何解决它吗?
标签: php session laravel curl cookies