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