我正在尝试创建一个小的PHP脚本,其中列出了所有用户ID,这些用户ID是关注/在Twitter上有句柄的朋友,为此我正在使用https://twitteroauth.com
当我使用"朋友"时,我可以将ID转换为文件本身。或"粉丝"单独地,但是当我试图将这个脚本移动到一个函数时,我得到了#34;致命错误:调用成员函数get()on null" (第19行)
因为此行
触发了错误" $ tweets = $ twitteroauth-> get($ type。' / ids',array(' screen_name' => $ term,' cursor' => $ next_cursor,' count' => 50)); "
正在函数中使用......
我使用了作曲家并尝试让它在一个函数之外工作..这两个主要文件是
的index.php
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
require_once 'config.php';
// Pass in arguments
if (PHP_SAPI === 'cli') {
$type = $argv[1];
$term = $argv[2];
}
else {
$type = $_GET['type'];
$term = $_GET['term'];
}
switch ($type){
case 'timeline':
include 'timeline.php';
break;
case 'followers':
case 'friends':
include 'f.php';
break;
case 'ratelimit':
include 'ratelimit.php';
break;
case 'users_followers':
case 'users_friends':
include 'users.php';
break;
case 'all':
// include 'timeline.php';
include 'f.php';
break;
}
?>
和f.php
<?php
function getFrindsFollowers($term, $type){
// create file to print follwer/friends id's to file
$file = "files/" . $term . '_' . $type . '.csv';
// set empty content to file
$content = null;
// set previous cursor
$previous_cursor = 0;
$next_cursor = -1;
$loop_num = 0;
// While statment for followers or friends calls.
while($next_cursor != $previous_cursor && $loop_num < 15 && $next_cursor != 0){
//use Abraham\TwitterOAuth\TwitterOAuth;
//$twitteroauth = new TwitterOAuth(consumer_key, consumer_secret, oauth_access_token, oauth_access_token_secret);
$tweets = $twitteroauth->get($type . '/ids', array ( 'screen_name' => $term, 'cursor' => $next_cursor, 'count' => 50));
// Pause the loop for 16 min after every 15th request
if ($loop_num % 15 == 0 && $loop_num > 15) {
echo "sleep mode";
sleep(960);
echo "sleep mode done";
}
// set cursors
$previous_cursor = $next_cursor;
//echo 'Previous cursor is ' . $previous_cursor;
//echo '\n Next cursor is ' . $next_cursor;
foreach($tweets as $key => $val) {
if($key == "ids"){
//echo $val[0];
foreach($val as $value){
$value . "\n";
$content .= ",\n" . $value;
}
}
if($key == "next_cursor"){
//echo "\n \n";
$next_cursor = $val;
}
}
$loop_num ++;
echo "Type is now " . $type . "\n";
echo "Loop is " . $loop_num . " for " . $type . "\n";
file_put_contents($file, $content);
}
}
getFrindsFollowers($term, $type);
&GT;
很可能是一个简单的修复方法,但我很欣赏任何有关如何在函数中使用get请求的指导。
答案 0 :(得分:0)
您在范围(http://php.net/manual/en/language.variables.scope.php)
方面遇到问题如果您已在函数外部定义了twitteroauth变量,请在其开头添加此行
global $twitteroauth;
这将允许您访问函数内的变量。