App \ Http \ Controllers \ CustomerController :: postOptOut()缺少参数1

问题描述 投票:1回答:1

我正在寻找一些关于如何清理这两种方法并解决此错误的建议?现在,getOptOut()正在向API发出GET请求以获取email_token并返回视图,postOptOut()正在发出POST请求,使用来自GET的email_token,并允许“客户”选择退出邮件列表然后重定向到客户家。

public function getOptOut(EmailOptingRequest $request)
{

    $customer = Customer::find(Auth::id());
    $email = $customer['attributes']['Email'];
    $token = "9asdfj48asdj48adja4r8";

    $client = new Client();

    $res = $client->request('GET', 
    'https://www.example.com/api/Services/Email/Opting', [
        'headers' => [
            'Accept' => 'application/json',
            'Authorization' => 'Bearer ' . $token
        ],
        'email' => $email,
        'http_errors' => false // add this to return errors in json
    ]);

    $emailToken = json_decode($res->getBody()->getContents(), true);
    $this->postOptOut($emailToken);

    return view('customer.email-opting', array(
        'customer' => $customer,
        'email' => $email,
        'token' => $token,
        'client' => $client,
        'res' => $res,
        'emailToken' => $emailToken
    ));
}

public function postOptOut($emailToken)
{
    $customer = Customer::find(Auth::id());
    $email_token = $emailToken[0]['token'];

    $client = new Client();

    $res = $client->request('POST', 'https://www.example.com/api/Services/Email/Opting', [
        'email_token' => $email_token,
        'category' => 'promotional',
        'status' => false
    ]);

    return view('customer.show', array(
        'customer' => $customer,
        'email_token' => $email_token,
        'client' => $client,
        'res' => $res ))
        ->with('success', 'You were removed from our mailing list.');
}

我的路线:

Route::get(   'customer/email-opting', 'CustomerController@getOptOut');
Route::post(  'customer/post-opt-out', 'CustomerController@postOptOut');

硬编码令牌是临时的。我遇到了GET和POST调用时间以及视图返回时的问题。谢谢!

php laravel api http guzzle
1个回答
1
投票
public function postOptOut(Request $request)
{
    $customer = Customer::find(Auth::id());
    $email_token = $request->emailToken[0]['token']; // this way you will get token

    $client = new Client();

    $res = $client->request('POST', 'https://www.example.com/api/Services/Email/Opting', [
        'email_token' => $email_token,
        'category' => 'promotional',
        'status' => false
    ]);

    return view('customer.show', array(
        'customer' => $customer,
        'email_token' => $email_token,
        'client' => $client,
        'res' => $res ))
        ->with('success', 'You were removed from our mailing list.');
}

试试这个

© www.soinside.com 2019 - 2024. All rights reserved.