卷曲与laravel返回会话已过期 [英] curl with laravel return session expired

查看:66
本文介绍了卷曲与laravel返回会话已过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel中使用curl,并且需要在控制器内部启动一个函数.

I use curl with laravel and I need to launch a function inside a controller.

但是当我放上

dd('test'); 

在名称空间之后打印.但是当我尝试使用该功能时,会话已过期

after the namespace it's print.but when I try with the function a have a session expired

这是卷曲的代码

// Préparation des données
  $data = [];
  $data["code__client"] = "VERTDIS13";
  $data["status"] = "90";
  $data["numero__incident"] = "INC544842";

  // On tranforme le tableau PHP en objet JSON
  $data = json_encode($data);

  // Envoi au serveur
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,"http://localhost:3000/notification/server");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "data=" . $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
  $server_output = curl_exec($ch);
  $error = curl_error($ch);
  if($error!=""){ dd($error);}
  curl_close($ch);


  // Ici pour tester j'affiche ce que le serveur renvoi
  return $server_output;

我尝试在控制器内部打印数据,但无法正常工作

inside the controller I tried to print data but it doesn't work

您看到问题了吗?

感谢帮助.

推荐答案

会话过期响应对我来说听起来像是对应用程序执行多个连续的curl请求.如果是这种情况,并且您的应用程序依赖于会话cookie,则可以创建一个简单的文本文件,该文件可对执行curl请求的脚本进行读写,并将其添加到您的代码中:

The session expired response sounds to me as if you are performing multiple consecutive curl requests to your application. If that's the case and your application depends on session cookies you can create a simple text file that is read and writeable to the script that does the curl requests, and add this to your code:

$cookie_file = "/the/path/to/your/cookiefile.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

$cookie_file中的路径更改为您创建并卷曲的文本文件的完整文件系统路径,将保留与响应一起返回的会话cookie,并在连续的请求中使用它们. 为此,我认为您应该删除该行

Change the path in $cookie_file to the full filesystem path of the text file you create and curl will persist session cookies that are being returned with responses and use them in consecutive requests. For this to work I think you should remove the line

curl_setopt($ch, CURLOPT_COOKIESESSION, true);

因为这将建议curl忽略文件中的cookie.

because this would advise curl to ignore the cookies in the file.

除了我认为的线条

curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true); 

也已过时,因为您没有在curl请求中提供基本的身份验证凭据.

is also obsolete, because you don't provide basic auth credentials in your curl request.

这篇关于卷曲与laravel返回会话已过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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