0

Drupal 有一个 gmail 联系人导入器模块,它已经非常接近工作了大约 7 个月了。我没有调试问题的能力,维护者消失了,我认为这是一个重要的工作。所以我求助于 StackOverflow。

问题:当您填写您的凭据并告诉脚本带回您的联系人时,默认情况下它会拉入 25。这很好。

当您告诉它通过更改您查询的 URL 来查找更多联系人时:

http://www.google.com/m8/feeds/contacts/default/thin  

对此:

http://www.google.com/m8/feeds/contacts/default/thin?max-results=1000

您收到以下致命错误:

致命错误:在第 97 行调用getAttribute()非对象上的成员函数path/to/site/sites/all/modules/dcl_importer/scripts/importGData.class.php

这是脚本:

class GDataMailer {
    static $url_ClientLogin = 'https://www.google.com/accounts/ClientLogin';
    static $url_Feed = 'http://www.google.com/m8/feeds/contacts/default/thin?max-results=65535';

    function newCurlSession($URL, $auth = null) {
        $curl = curl_init();

        $opts = array(
            CURLOPT_URL => $URL,
            CURLOPT_REFERER => '',
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
        );
        if (null != $auth) {
            $opts[CURLOPT_HTTPHEADER] = array(
               'Authorization: GoogleLogin auth='.$auth,
            );
        }
        curl_setopt_array($curl, $opts);
        return $curl;
    }

    function useCurlForPost($curl, $params) {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        return $curl;
    }

    function getAuthToken($login, $password) {
        $curl = $this->useCurlForPost($this->newCurlSession(self::$url_ClientLogin), array(
           'accountType' => 'HOSTED_OR_GOOGLE',
           'Email' => $login,
           'Passwd' => $password,
           'service' => 'cp',
           'source' => 'Drupal-Contact-Importer',
        ));
        $resp = curl_exec($curl);

        // Look for the important stuff:
        preg_match_all('/Auth=([^\s]*)/si', $resp, $matches);
        if (isset($matches[1][0])) {
            return $matches[1][0];
        } else {
           return false;
        }
    }

    function getAddressbook($login, $password) {
        // check if username and password was given:
        if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
        {
            $args = func_get_args();
            drupal_set_message('Incorrect login information given: '.print_r($args, true), 'error');
            return false;
        }

        // Get the GData auth token:
        $auth = $this->getAuthToken($login, $password);
        if (false === $auth) {
            drupal_set_message('Login failed', 'error');
            return false;
        }        

        $curl = $this->newCurlSession(self::$url_Feed, $auth);
        $data = curl_exec($curl);

        $doc = new DOMDocument();
        $doc->loadXML($data);
        $path = new DOMXPath($doc);
        $path->registerNamespace('a', 'http://www.w3.org/2005/Atom');
        $path->registerNamespace('gd', 'http://schemas.google.com/g/2005');
        $nodes = $path->query('//a:entry');
        $num = $nodes->length;

        $contacts = array();
        for ($x = 0; $x < $num; $x++) {
            $entry = $nodes->item($x);
            $tnodes = $path->query('a:title', $entry);
            $nnode = $tnodes->item(0);
            $name = $nnode->textContent;
            $enodes = $path->query('gd:email', $entry);
            $mnode = $enodes->item(0);
            $email = $mnode->getAttribute('address');
            // NOTE: Keep in mind that $mnode->getAttribute('rel') tells you what kind of email it is.
            // NOTE: Also remember that there can be multiple emails per entry!
            if (empty($name)) {
                $contacts[] = $email;
            } else {
                $contacts[$name] = $email;
            }
        }        

        return $contacts;
    }
}

97 号线是$email = $mnode->getAttribute('address');接近尽头的那条线。

我可以在这里更改什么以不再出现该错误并使其适用于 Drupal 社区?我想提取一个人的所有联系人列表,而不仅仅是 25 个。我认为如果我发送的号码足够高,它就会足够接近可以通过。

4

1 回答 1

0

在不直接测试的情况下,我会尝试用以下代码替换第 96 - 104 行:

$mnode = $enodes->item(0);
if (isset($mnode) && is_object($mnode)) {
    $email = $mnode->getAttribute('address');
    // NOTE: Keep in mind that $mnode->getAttribute('rel') tells you what kind of email it is.
    // NOTE: Also remember that there can be multiple emails per entry!
    if (!empty($email)) {
        if (empty($name)) {
            $contacts[] = $email;
        } else {
            $contacts[$name] = $email;
        }
    }
}

gd:email 是根据Google Data API的可选元素。它在 Gmail 的实现中也是可选的。您正在使用的模块假定它存在并且当它不存在时失败。

根据注释:注释,每个联系人的多个电子邮件地址仍未处理。

于 2009-05-06T18:16:37.127 回答