2013-06-18 44 views
1

阵列我有这个功能,我得到这个错误Zend的PHP函数的错误 - 警告:array_merge():参数#1不

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; 
       } 
      } 
    } 

任何提示,为什么我得到这个错误?

+4

您是否尝试过调试之前是空的?使用'var_dump($ followers ['ids'])来检查;'如果是数组或不是。 – Rikesh

回答

0

看起来你连接到Twitter API时没有进行身份验证。如果未通过身份验证,则两个链接的结果都是{"errors":[{"message":"Bad Authentication data","code":215}]},在这种情况下,$followers['ids']不会是数组,因为它不存在。

Twitter's API documentation包含认证信息。

如果这不是问题,我很抱歉,但它看起来像你的代码判断。

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; 
      } 
     } 
     } 
    } 
    } 
} 
相关问题