似乎无法从联系人 API 检索除姓名和电子邮件以外的其他内容



我很确定gd$phoneNumbergd$citygd$streetgd$country是无效的。我只是找不到如何称呼这些变量。

所有变量都只返回 NULL。请参阅下面的我的vardump

["name"]=> string(0) ""
["email"]=> string(18) "xxxx@xxxxxxx.com"
["phoneNumber"]=> NULL
["city"]=> NULL
["street"]=> NULL
["country"]=> NULL

此外,我只能在本地主机中从联系人API中检索信息。

当我在xxxxxx.appspot.com网站上时,它不会检索信息,但它会显示表格标题。

问题

如何使用联系人 API 检索除姓名和电子邮件地址之外的更多内容?

主.php

<a href="<?php echo $googleImportUrl; ?>"> Import google contacts </a>
<table class="table table-striped table-condensed">
    <thead>
        <tr>
            <th>Naam </th>
            <th>Email </th>
            <th>Telefoon </th>
            <th>Stad </th>
            <th>Adres </th>
            <th>Land </th>
        </tr>
    </thead>
    <tbody>
    <?php
        foreach( $google_contacts as $contacts ){
            echo'<tr>';
            echo'<td>'. $contacts['name']."</td>";
            echo'<td>'. $contacts['email'].'</td>';
            echo'<td>'. $contacts['phoneNumber']."</td>";
            echo'<td>'. $contacts['city'].'</td>';
            echo'<td>'. $contacts['street']."</td>";
            echo'<td>'. $contacts['country'].'</td>';
            echo'</tr>';
        }?>
    </tbody>
</table>

响应回调.php

$test = 'HEY';
require_once 'google-api-php-client-1-master/src/Google/autoload.php';
$google_client_id = 'xxxxxx-xxxxxx.apps.googleusercontent.com';
$google_client_secret = 'xxxxxxx';
$google_redirect_uri = 'http://localhost:9080';
//$google_redirect_uri = 'https://importcontacts-1216.appspot.com';
//setup new google client
$client = new Google_Client();
$client -> setApplicationName('ImportContacts');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');
$client -> setScopes('https://www.google.com/m8/feeds');
$googleImportUrl = $client -> createAuthUrl();
function curl( $url, $post = "" ){
    $curl = curl_init();
    $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
    curl_setopt($curl, CURLOPT_URL, $url);
    //The URL to fetch. This can also be set when initializing a session with curl_init().
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
    //The number of seconds to wait while trying to connect.
    if( $post != "" ){
        curl_setopt($curl, CURLOPT_POST, 5);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }
    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
    //The contents of the "User-Agent: " header to be used in a HTTP request.
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    //To follow any "Location: " header that the server sends as part of the HTTP header.
    curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
    //To automatically set the Referer: field in requests where it follows a Location: redirect.
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    //The maximum number of seconds to allow cURL functions to execute.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    //To stop cURL from verifying the peer's certificate.
    $contents = curl_exec($curl);
    curl_close($curl);
    return $contents;
}
//google response with contact. We set a session and redirect back
if( isset( $_GET['code'] ) ){
    $auth_code = $_GET["code"];
    $_SESSION['google_code'] = $auth_code;
    header('Location: ' . $google_redirect_uri);
}
if( isset( $_SESSION['google_code'] ) ){
    $auth_code = $_SESSION['google_code'];
    $max_results = 200;
    $fields = array(
        'code'=>  urlencode($auth_code),
        'client_id'=>  urlencode($google_client_id),
        'client_secret'=>  urlencode($google_client_secret),
        'redirect_uri'=>  urlencode($google_redirect_uri),
        'grant_type'=>  urlencode('authorization_code')
    );
    $post = '';
    foreach( $fields as $key=>$value ){
        $post .= $key.'='.$value.'&';
    }
    $post = rtrim($post,'&');
    $result = curl('https://accounts.google.com/o/oauth2/token',$post);
    $response =  json_decode($result);
    $accesstoken = $response->access_token;
    $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
    $xmlresponse =  curl($url);
    $contacts = json_decode($xmlresponse,true);
    $return = array();
    if( !empty( $contacts['feed']['entry'] ) ){
        foreach( $contacts['feed']['entry'] as $contact ){
            //retrieve Name and email address
            $return[] = array(
                'name'=> $contact['title']['$t'],
                'email' => $contact['gd$email'][0]['address'],
                'phoneNumber' => $contact['gd$phoneNumber'][0]['address'],
                'city' => $contact['gd$city'][0]['address'],
                'street' => $contact['gd$street'][0]['address'],
                'country' => $contact['gd$country'][0]['address'],
            );
        }
    }
    $google_contacts = $return;
    unset( $_SESSION['google_code'] );
}
if( !empty( $contacts['feed']['entry'] ) ){
    foreach( $contacts['feed']['entry'] as $contact ){
        //retrieve Name and email address
        $return[] = array(
            'name'=> $contact['title']['$t'],
            'email' => $contact['gd$email'][0]['address'],
            'phoneNumber' => $contact['gd$phoneNumber'][0]['address'],
            'city' => $contact['gd$city'][0]['address'],
            'street' => $contact['gd$street'][0]['address'],
            'country' => $contact['gd$country'][0]['address'],
            '' => $contact['gd$']
        );
        //retrieve user photo
        if( isset( $contact['link'][0]['href'] ) ){
            $url =   $contact['link'][0]['href'];
            $url = $url . '&access_token=' . urlencode($accesstoken);
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_TIMEOUT, 15);
            curl_setopt($curl, CURLOPT_VERBOSE, true);
            $image = curl_exec($curl);
            curl_close($curl);
        }
        $return['image'] = $image;
        echo '';  
    }
}

将返回数组更改为:

$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'],
            );

相关内容

  • 没有找到相关文章

最新更新