【发布时间】:2017-05-26 07:19:05
【问题描述】:
我使用 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');
}
【问题讨论】:
-
从索引函数返回代码并访问索引函数 insdie 图片视图
-
@Exprator 你能解释更多吗?
-
在索引函数的末尾返回 $code;并在图片视图中调用函数 index() 然后你会得到代码
-
在任何函数开始之前定义一个名为 access_token (
private access_token) 的私有变量,然后您可以使用$this->access_token访问该变量(分配给它或任何东西);
标签: php laravel instagram-api guzzle