我有这个功能并且收到此错误
Warning: array_merge(): Argument #1 is not an array in
$diff = array_merge($followers['ids'], $friends['ids']);
然后
Invalid argument supplied for foreach() in
功能:
public function addtosystemAction(){
$this->_tweeps = new Application_Model_Tweeps();
$http = new Zend_Http_Client();
$http->setUri('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=testuser');
$followers = Zend_Json::decode($http->request()->getBody(), true);
$http->setUri('http://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=testuser');
$friends = Zend_Json::decode($http->request()->getBody(), true);
$diff = array_merge($followers['ids'], $friends['ids']);
$resultArray = array();
foreach ($diff as $id){
if(FALSE == $this->_tweeps->checkExisting($id)){
$resultArray[] = $id;
if(count($resultArray) == 50){
break;
}
}
}
为什么我收到此错误提示?
答案 0 :(得分:1)
在传递给函数
之前,你应该检查数组是否为空试试这个
public function addtosystemAction(){
$this->_tweeps = new Application_Model_Tweeps();
$http = new Zend_Http_Client();
$http->setUri('http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=testuser');
$followers = Zend_Json::decode($http->request()->getBody(), true);
$http->setUri('http://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=testuser');
$friends = Zend_Json::decode($http->request()->getBody(), true);
if( (!empty($followers['ids'])) && (!empty($friends['ids'])) ){
$diff = array_merge($followers['ids'], $friends['ids']);
$resultArray = array();
if(!empty($diff)){
foreach ($diff as $id){
if(FALSE == $this->_tweeps->checkExisting($id)){
$resultArray[] = $id;
if(count($resultArray) == 50){
break;
}
}
}
}
}
}
答案 1 :(得分:0)
在连接到Twitter API时,您似乎没有进行身份验证。未经过身份验证时,这两个链接都会生成{"errors":[{"message":"Bad Authentication data","code":215}]}
,在这种情况下,$followers['ids']
将不会是数组,因为它不存在。
Twitter's API documentation包含有关身份验证的信息。
如果这不是问题,我道歉,但看起来就像你的代码判断。