我使用api Instagram,我在indexx函数中使用access_token变量,它是服务器发送的json结果,现在我试图在图片函数中使用这个变量并用Guzzle将HTTP发送到服务器我该怎么办? ? 我的控制器有这个代码:
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');
}
答案 0 :(得分:0)
另一种方法是写入config
并从中读取
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);
}