为什么 PHP 在使用 Instagram PHP API 时不保存会话?

发布于 2025-01-06 01:25:00 字数 900 浏览 1 评论 0原文

我正在尝试使用此处的 php 库创建一个 Instagram 应用程序(文件不是很长,不用担心):

http://pastie.org/3391787

我正在尝试使用 OAUTH 来授权用户,它工作得很好,并且授权的(当前用户)可以看到他的信息(如图像、姓名)等等...),但是当我刷新页面时,一切都会消失,我只剩下空白数据,这是代码 -

// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

$instagram->setAccessToken($_SESSION['InstagramAccessToken']);
$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);

该代码第一次运行正常,但是当您刷新页面时,不会检索信息,这只能意味着一件事,即会话或 cookie 未正确设置,这就是 getUser 方法未检索信息的原因,

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

请您解释一下这个问题,我已经尝试了 4 个小时!

I'm trying to create a instagram app using the php library which is here(its not very long file, don't worry):

http://pastie.org/3391787

I'm trying to authorise the user by using OAUTH, which works perfectly, and the authorised(current user) can see his information(like Image, name etc...), but when i refresh the page it all goes and I'm left with blank data, heres the code -

// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

$instagram->setAccessToken($_SESSION['InstagramAccessToken']);
$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);

this code works fine the first time round, but when you refresh the page, the information is not being retrieved, and that could only mean one thing, that the session or the cookie is not set properly and thats why the getUser method is not retrieving the information,

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

please can you shed some light into this problem, i have been trying for 4 hours!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2025-01-13 01:25:00

在您的每个请求中,您都通过这一行覆盖当前会话数据:

$_SESSION['InstagramAccessToken'] = $accessToken;

最有可能的是它应该是这样的:

$instagram = new Instagram($config);

if (!empty($_SESSION['InstagramAccessToken'])) {
    $accessToken = $instagram->getAccessToken();
    $_SESSION['InstagramAccessToken'] = $accessToken;
} else {
    $instagram->setAccessToken($_SESSION['InstagramAccessToken']);
}

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);

On each your request you overwrite current session data with nothing by this line:

$_SESSION['InstagramAccessToken'] = $accessToken;

Most likely this is how it should look like:

$instagram = new Instagram($config);

if (!empty($_SESSION['InstagramAccessToken'])) {
    $accessToken = $instagram->getAccessToken();
    $_SESSION['InstagramAccessToken'] = $accessToken;
} else {
    $instagram->setAccessToken($_SESSION['InstagramAccessToken']);
}

$userinfo = $instagram->getUser($_SESSION['InstagramAccessToken']);

// After getting the response, let's iterate the payload
$ures = json_decode($userinfo, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文