-1

我试图通过搜索他们的用户名来获取 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"
        }
    }
}
4

2 回答 2

0

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

于 2019-08-28T01:04:43.673 回答
-1
<i>
if ($response){
    foreach (json_decode($searchresult)->data as $username) {
$src = $item->images->standard_resolution->url;
        $username = $item->username;
      );
    }
}
</i>
于 2013-09-22T18:07:56.597 回答