【问题标题】:How can I retrieve an ID from Google Contacts API?如何从 Google Contacts API 检索 ID?
【发布时间】:2016-02-16 10:50:51
【问题描述】:

我正在尝试从 Google Contacts API v3 检索 ID。我能够检索到最后带有 id 的整个 url。

http://www.google.com/m8/feeds/contacts/mike%40xxxxxxx.com/base/xxxthisistheidxxxx

所以我要么以某种方式拆分了 url,要么我需要找到一种完全不同的方式来检索 ID。

if (!empty($contacts['feed']['entry'])) {
    foreach($contacts['feed']['entry'] as $contact) {
        //retrieve Name and email address
        $return[] = array (
            'firstname'=> $contact['gd$name']['gd$givenName']['$t'],
            'lastname'=> $contact['gd$name']['gd$familyName']['$t'],
            'email' => $contact['gd$email'][0]['address'],
            'phoneNumber' => $contact['gd$phoneNumber'][0]['$t'],
            'city' => $contact['gd$structuredPostalAddress'][0]['gd$city']['$t'],
            'street' => $contact['gd$structuredPostalAddress'][0]['gd$street']['$t'],
            'country' => $contact['gd$structuredPostalAddress'][0]['gd$country']['$t'],
            'birthday' => $contact['gContact$birthday']['when'],
            'id' => $contact['id']['$t'],
        );
    }
}

【问题讨论】:

    标签: php google-app-engine google-api google-contacts-api


    【解决方案1】:

    你可以做的是在/'s 上拆分 url,而不是获取结果的最后一个索引。

    例子:

    $url = "http://www.google.com/m8/feeds/contacts/mike%40xxxxxxx.com/base/xxxthisistheidxxxx";
    
    $exploded = explode('/', $url);
    echo end($exploded); //Echoes the last index.
    

    【讨论】:

      【解决方案2】:

      使用正则表达式:

      preg_match("/[^\/]+$/", "http://www.google.com/m8/feeds/contacts/mike%40xxxxxxx.com/base/xxxthisistheidxxxx", $matches);
      $last_word = $matches[0]; // test
      

      【讨论】:

      • 尽管这是解决方案,但对于这类问题,正则表达式有点重。即使您不会注意到太大的不同,这仍然是一种不好的做法。
      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多