php - 无法在具有 laravel 和 Angular 的站点上的 api 请求中动态设置 cookie
问题描述
大家好,我有一个应用程序,我需要在其中发出 API 请求,并且该应用程序由 laravel 和 Angular 组成。Laravel 用于后端,Angular 用于前端。API 用于为用户订阅特定任务。在我的应用程序上,我需要发出一个 API 请求,该请求需要从后端生成的 cookie(很可能我不确定)。因此,这些 cookie 使我的 API 能够正常工作。我已经准备好了所有的东西,但是I can't set the cookies dynamically
。问题是我不确定这些 cookie 是从哪里生成的。我创建了一个自定义函数,该函数使用某些特定参数调用 API,然后它只是以用户订阅的权限订阅用户。
我的应用程序工作流程如下
- 在角度上,我提交了付款表格
- 表单提交后,我的应用程序进入第三方提供的支付沙箱(这是一个完全不同的服务器)
- 然后在成功支付交易后,它会重定向到我的 laravel 应用程序并调用我制作的自定义函数并尝试使用用户支付的功能订阅用户。
在最后一步,我无法获得让用户订阅他们支付的计划所需的 cookie。这次我需要从 API 中获取 cookie,我可以通过手动发出请求来获取该 cookie。因为这是一个付款程序,我不能让它手动完成,整个过程需要自动进行。
我的 API 没问题The problem is that I can not make the cookies dynamically and also I am not being able to use those dynamically generated cookies each time a request is made
。
- 以下是主要的api函数
/**
* Create a new subscription.
*
* @return JsonResponse
*/
public function store()
{
$this->authorize('update', Subscription::class);
$this->validate($this->request, [
'user_id' => 'required|exists:users,id|unique:subscriptions',
'renews_at' => 'required_without:ends_at|date|nullable',
'ends_at' => 'required_without:renews_at|date|nullable',
'plan_id' => 'required|integer|exists:billing_plans,id',
'description' => 'string|nullable',
]);
$subscription = $this->subscription->create($this->request->all());
return $this->success(['subscription' => $subscription]);
console.log('running');
}
- 以下是我的自定义功能
public function apicall(Request $request){
$curl = curl_init();
$xsrf_token = request()->Cookie("X-XSRF-TOKEN");
$cookie = request()->Cookie("Cookie");
curl_setopt_array($curl, array(
CURLOPT_URL => 'localhost:4200/secure/billing/subscriptions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"user_id": 3,
"renews_at": "2021-01-29 18:00:00",
"plan_id": 1,
"description": "null"
}',
CURLOPT_HTTPHEADER => array(
$xsrf_token,
$cookie,
'Host: localhost:4200',
'Referer: http://localhost:4200/admin/subscriptions?page=1&per_page=15',
'Origin: http://localhost:4200',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return redirect('http://localhost:4200/dashboard')->with('status', 'Success');
}
- 在 web.php 上,我已经配置了路由和其他所有内容。我的 API 和支付重定向都运行良好,但用户没有被订阅,或者我可以说 API 可能无法验证请求的所有者或请求的验证,因此它无法给我任何200响应。它只是给了我302的
found
标签。那么应该如何动态设置cookie,让系统的订阅cookie可以匹配到请求cookie呢?
谁能帮我这个?我需要尽快解决这个问题。
解决方案
推荐阅读
- java - 练习 JUnit 比较字符串的奇怪行为
- javascript - JavaScript - 将单个值从数组传递给变量
- python-3.x - 插入树视图
- python - 使用拆分和替换来安排数据框,反斜杠有问题
- list - 如何排除此正则表达式的其他匹配项?
- javascript - TypeError: (0 , _dataActions.loginUser) 不是函数
- javascript - 从 Div 获取匹配的文本
- perl - 如何为 perl 中的 Getopt::Std 中的一个选项提供更多参数(以空格分隔)?
- python - 部分付款后如何将发票标记为已付款
- javascript - 在不调用 click 命令的情况下使用 Cypress 单击按钮?