简体   繁体   English

带有PHP的Twitter API-无法使用abrahams twitter oauth库将好友/ id请求的结果传递给用户/查找请求

[英]Twitter API with PHP - can't pass results of friends/ids request to users/lookup request using abrahams twitter oauth library

I'm having problems passing the results of a friends/ids request to a users/lookup request while using Abraham's Twitter OAuth ( https://twitteroauth.com/ ) PHP library to access the Twitter REST API. 我在使用Abraham的Twitter OAuth( https://twitteroauth.com/ )PHP库访问Twitter REST API时,将friends/ids请求的结果传递给users/lookup请求时遇到问题。

After authenticating, I get an account's friends list as user ids: 验证后,我得到一个帐户的朋友列表作为用户ID:

$content = $connection->get("followers/ids", ["screen_name" => $input]);

Then I'm creating a comma separated list: 然后,我创建一个逗号分隔的列表:

foreach ($content as $user) {
$userlist = implode(', ', $user);

And then I'm passing this to a users/lookup request: 然后将其传递给用户/查找请求:

$output = $connection->post("users/lookup", ["user_id" => $userlist]);

This gives a code 17 error which I understand to mean no such account was identified by Twitter. 这给出了代码17错误,据我理解,这意味着Twitter没有识别出此类帐户。 Outputting the imploded $userlist shows this step is working ok. 输出内爆的$userlist表示此步骤工作正常。

If I define $userlist myself then the subsequent call to users/lookup works fine. 如果我自己定义$userlist则随后对users/lookup调用工作正常。 For example: 例如:

$userlist = "820310862045052930, 806614673474912256, 745020013837434880, 789205729123065860, 717272899741204480, 2523773164, 763810846929719296, 817061186705457152, 806495626670186496, 1935657786, 813858305282109442, 224295002, 24016369, 719472791200739328, 3292608016, 544394440, 338499233, 704776216, 1080910670, 2162932007, 15700673, 2212757984, 375238808, 2949937593, 244523746, 145021177, 4195801821, 799570638847561728"

I've tried converting the results of the first request (friends/ids) to array: 我试过将第一个请求(朋友/ id)的结果转换为数组:

 $contentarray = json_decode(json_encode($content), True);

but this makes no difference. 但这没什么区别。 I've also tried passing the list of ids as an array (and defining the $userlist as such in the request). 我还尝试过将ID列表作为数组传递(并在请求中定义$ userlist)。 Wrapping $userlist in quotes doesn't work either and it makes no difference if I use GET or POST. $userlist括在引号中也不起作用,并且如果我使用GET或POST也没有区别。 Similarly, creating another array of just user_ids (excluding the cursors) and creating a comma separated list from this does not make a difference. 同样,仅创建另一个user_ids数组(不包括游标),并由此创建一个逗号分隔的列表也没有什么区别。

Twitter OAuth is usually so simple and intuitive to use but I've spent hours on this and am getting nowhere. Twitter OAuth通常使用起来非常简单直观,但是我已经花了数小时,却一无所获。 Can anyone help out with where I'm going wrong? 谁能帮我解决我的问题?

So it turns out the problem was with my foreach loop. 因此事实证明问题出在我的foreach循环上。 Hat tip to hobbes3 for this answer . hobbes3提示这个问题

After authenticating: 认证后:

// get the user's followers
$content = $connection->get("followers/ids", ["screen_name" => $input]);

// replace stdclass object with array
 $content_as_array = json_decode(json_encode($content), True);

//create new array with foreach
$content_as_new_array = array();
foreach($content_as_array as $user) {
foreach($user as $newuser) {
array_push($content_as_new_array, $newuser);
  }
}

// implode this new array
$userlist = implode(", ", $content_as_new_array);

// and pass this new array to API

 $output = $connection->post("users/lookup", ["user_id" => $userlist]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM