简体   繁体   中英

two function with one variable in controller of laravel5.4

I work with api Instagram and I'm using access_token variable in indexx function that it is json result that server send,Now I'm Trying to use this variable in picture function and send HTTP to server with Guzzle what Can I do? my controller has this Code:

public function indexx(Request $request)
{
    $code = $this->code = $request->get('code');

    try {
        $client = new Client();
        $res = $client->request  ('POST', 'https://api.instagram.com/oauth/access_token', [
            'form_params' => [
                'client_id' => 'client_id' ,
                'client_secret' =>  'client_secret',
                'grant_type' => 'authorization_code',
                'redirect_uri' => 'http://hanie-asemi.ir/laravel/instagram/test',
                'code' => $code
            ]
        ]);
        $status = $res->getStatusCode();
        if ($status == 200) {
            $body = $res->getBody();
            $obj = \GuzzleHttp\json_decode($body);

            $access_token=$obj->access_token;
            echo view('json')->with($access_token);

        } else {
            echo "status is not 200";
        }

    } catch (BadResponseException $e) {
        echo "error1";
    }

}

public function picture()
{
    $client = new Client();
    $res2 = $client->request  ('GET', 'https://api.instagram.com/v1/users/self/media/recent/?access_token=Access_token');
}

Another way would be to writing to the config and reading from it

public function indexx(Request $request)
{
    ...
    $access_token = $obj->access_token;
    config(['app.access_token' => $access_token]);
    ...
}

public function picture()
{
    $client = new Client();
    $access_token = config('app.access_token', 'fallback token');
    $res2 = $client->request('GET', 'https://api.instagram.com/v1/users/self/media/recent/?access_token='.$access_token);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM