I recently upgraded to 6.x-3.0-beta2 since Twitter discontinued support for basic authentication. However, since then I have been consistently seeing this error on my Logs.

Invalid argument supplied for foreach() in /home/kiran/public_html/sites/all/modules/twitter/twitter.lib.php

Also, I have noticed that the latest twitter updates fail to import. All was working fine on 6.x-2.6. Hence, I am not sure if this is related to the version upgrade or something on Twitter's side with their OAuth mechanism.

Any help would be greatly appreciated.

Regards,
KH

Comments

kjholla’s picture

Title: Invalid argument supplied for foreach() in /home/kiran/public_html/sites/all/modules/twitter/twitter.lib.php » Twitter status import fails on cron run as anonymous user

On reading up more on this issue, I see that the problem already has a fix at http://drupal.org/node/685964.

Applying the patch provided in that thread will probably fix the Invalid argument error coming up in the log. However, what bothers me more is why is the call to the twitter api failing?

I have consistently seen that all calls made via the cron to the twitter api fail. However, if I log on as the user to whom the twitter account belongs and then run the cron manually, the import succeeds without the error.

This tells me that something is wrong with the authentication when the cron is running as an anonymous user, which causes the twitter api call to fail each time.

Could someone help?

Regards,
KH

kjholla’s picture

OK. This is my bad.

I just realized that the fix provided at http://drupal.org/node/685964 will work. I have applied it and it does work.

The cron run eventually runs the following function in twitter.lib.php


  /**
   * Get an array of TwitterStatus objects from an API endpoint
   */
  protected function get_statuses($path, $params = array(), $use_auth = FALSE) {
    $values = $this->call($path, $params, 'GET', $use_auth);
    $statuses = array();
    foreach ($values as $status) {
      $statuses[] = new TwitterStatus($status);
    }
    return $statuses;
  }

Here, if the twitter api call returns no new statuses (say, if the cron runs before any additional twitter updates have been made), it results in the Invalid argument error that I earlier mentioned as there are no new twitter updates to import.

By applying the patch provided at http://drupal.org/node/685964, this function is changed to:


  /**
   * Get an array of TwitterStatus objects from an API endpoint
   */
  protected function get_statuses($path, $params = array(), $use_auth = FALSE) {
    $values = $this->call($path, $params, 'GET', $use_auth);
    // Check on successfull call
    if ($values) {
      $statuses = array();
      foreach ($values as $status) {
        $statuses[] = new TwitterStatus($status);
      }
      return $statuses;
    }
    // Call might return FALSE , e.g. on failed authentication
    else {
      // As call allready throws an exception, we can return an empty array to
      // break no code.
      return array();
    }
  }

Here, the foreach loop is executed only if the twitter api call has returned something. I realized that the twitter api call not returning any rows wasn't a problem since there were no new twitter updates in the first place.

Apologies for being daft about this. But, hey, I figured it out in the end.

Regards,
KH

kjholla’s picture

Status: Active » Closed (duplicate)

Closing as this is effectively a duplicate of http://drupal.org/node/685964