【问题标题】:Instagram API get user ID with PHPInstagram API 使用 PHP 获取用户 ID
【发布时间】:2013-08-11 12:14:39
【问题描述】:

我试图通过搜索 Instagram 用户的用户名来获取他们的用户 ID,但是我的页面返回:

警告:为 foreach() 提供的参数无效

并且用户ID也返回为“00000000”

我的代码如下所示:

<?php
$username=preg_replace('[^0-9a-zA-Z_]',"",$_POST['username']); // Sanitize username
$userid = getInstaID($username); //Should return the userID for the username.

function getInstaID($username) {

$searchurl = "https://api.instagram.com/v1/users/search?q={$username}&access_token={$accessToken}";
$search_response = file_get_contents($searchurl);
$searchresult = json_decode($search_response);

foreach($searchresult->data as $user)
{
    if($user->username == $username)
    {
        return $user->id;
    }
}
return '00000000'; // Return this ID if no user is found.
}
?>

(我不得不将三个搜索变量移出函数+我选择了一个随机用户名)的输出

var_dump($searchresult);

object(stdClass)#2 (2) {
    ["meta"]=> object(stdClass)#4 (1) {
        ["code"]=> int(200)
    }
    ["data"]=> array(1) {
        [0]=> object(stdClass)#5 (6) {
            ["username"]=> string(10) "s3odhunter"
            ["bio"]=> string(155) "Twitter:s3odhunter متزوج وأب لأجمل طفلين بالعالم I'm king of the jungle ???? Muslim The most beautiful women in the world ????????"
            ["website"]=> string(27) "http://youtu.be/kBFWH-7pIS4"
            ["profile_picture"]=> string(75) "http://images.ak.instagram.com/profiles/profile_1104654_75sq_1370686814.jpg"
            ["full_name"]=> string(23) "سعود المخيال"
            ["id"]=> string(7) "1104654"
        }
    }
}

【问题讨论】:

  • 您确定是getInstaID() 函数中的foreach 构造导致了错误吗?因为如果$searchresult 真的像你发布的那样,那么应该没有这样的错误。代码:phpfiddle.org/main/code/znq-siz 结果:phpfiddle.org/api/run/znq-siz
  • 我不完全确定,但它确实会引发错误。如果它看起来像这样:foreach($searchresult as $user) PHP 不再抛出错误,但是该函数仍然返回 00000000。我会试试你的代码,看看它是否有效。
  • 好的 - 我发现了它为什么不起作用。该函数正在访问全局变量。看来我必须阅读变量和范围。

标签: php json api instagram


【解决方案1】:

如果您想仔细检查 API 返回的内容,您可以随时使用https://www.thekeygram.com/find-instagram-user-id/。它还提供用户名的用户 ID,但没有代码。

【讨论】:

    【解决方案2】:
    <i>
    if ($response){
        foreach (json_decode($searchresult)->data as $username) {
    $src = $item->images->standard_resolution->url;
            $username = $item->username;
          );
        }
    }
    </i>
    

    【讨论】:

    • 请避免复制/粘贴代码并详细说明您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多