简体   繁体   中英

Zend php function error - Warning: array_merge(): Argument #1 is not an array in

I've this function and am getting this error

Warning: array_merge(): Argument #1 is not an array in

$diff = array_merge($followers['ids'], $friends['ids']);

then

Invalid argument supplied for foreach() in 

Function:

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

Any tips why I'm getting this error ?

you should check if array is empty before passing to the function

Try this

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

It looks like you're not authenticating when connecting to the Twitter API. Both of your links result in {"errors":[{"message":"Bad Authentication data","code":215}]} when not authenticated, and in that case, $followers['ids'] won't be an array because it doesn't exist.

Twitter's API documentation contains information on authentication.

If that's not the issue, I apologise, but it looks like it judging by your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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