【问题标题】:set session when send posts data from external website via curl to action controller通过 curl 从外部网站向动作控制器发送数据时设置会话
【发布时间】:2018-06-04 16:16:04
【问题描述】:

我在我的家庭控制器中使用代码:

public function myAction1(Request $request)
{
   $request->session()->put('redirect', $_POST['redirect']);
}

public function myAction2(Request $request)
{
   $request->session()->get('redirect');
}

这是我route 的一部分:

Route::get('pGetway/send', 'HomeController@myAction1')->middleware('cors');
Route::post('pGetway/send', 'HomeController@myAction1')->middleware('cors');

Route::get('pGetway/verify', 'HomeController@myAction2')->middleware('cors');
Route::post('pGetway/verify', 'HomeController@myAction2')->middleware('cors');

当我使用来自浏览器的 HTTP 请求时,session 运行良好,但是当通过 curl 从其他网站发送数据时,它不起作用意味着会话没有设置!!

我想设置会话并从其他操作中使用它,例如在myAction2

这是 curl 请求帖子:

$params = ['redirect'=>'http://example.com'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8000/pGetway/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

$response = curl_exec($ch);
curl_close($ch);

谢谢

【问题讨论】:

    标签: php session laravel-5 laravel-session


    【解决方案1】:

    你应该设置 curl cookie 文件和 cookies jar 以便在会话中使用它

    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    

    【讨论】:

    • 谢谢,您能否进一步解释一下。如何设置cookie并从动作控制器中获取?
    • curl 为您提供了一些无需浏览器即可在本地使用 cookie 的方法。示例
    • curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie"));
    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多