2012-11-04 3 views
1

새로운 google_api_client PHP 라이브러리에 여전히 문제가 있습니다. 사용자의 연락처를 검색하려고합니다.PHP Google API 클라이언트 v3 연락처 받기

나는 올바른 해결책에 아주 가깝다. 나는 모든 결과를 얻었지만 파싱 할 수 없다는 것을 의미한다.

아마 XML 파서에 강하지 않기 때문일 수 있습니다. 테스트 및 테스트 후 ... 나는이 (Google의 예제 파일을 기반으로) 솔루션을 얻을 : 내 연락처의 전체 이름이 저장되어있는 제목 필드를 얻을 수있는 $ 응답에서

... 
$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full");   
$val = $client->getIo()->authenticatedRequest($req); 
$response = simplexml_load_string($val->getResponseBody()); 

foreach($response->entry as $entry) 
{ 
    $child = $entry->children("http://schemas.google.com/g/2005"); 
    $mail_info = $child->attributes(); 
} 
... 

을하고, $에서 mail_info 전자 메일 주소를 얻을 때 주소 필드를 볼 수있는 개체가 있습니다.

회사 이름, 주소 ... 전화 번호 ... 사진을 원하면 어떻게 될까요? 이 모든 정보는 어디에 있습니까?

Google 응답을 훌륭하고 깨끗한 솔루션으로 어떻게 사용할 수 있습니까?

누구나 저에게 도움을 줄 수 있습니다. 안녕

답변

6

XML 대신 JSON을 요청하는 데 도움이 된 것은 무엇입니까? google에 대한 요청으로 URL 끝에 ?alt=json을 추가하십시오.

$req = new apiHttpRequest("https://www.google.com/m8/feeds/contacts/default/full?alt=json");   
$val = $client->getIo()->authenticatedRequest($req); 
$string = $val->getResponseBody(); 
$phparray = json_decode($string); 

물론 당신이 원하는 것을 얻으려면 아이의 놀이는 아니지만 PHP 배열로 작업하는 것이 더 쉽습니다.

는 완성도를 위해 이것은 우리가 모두 아마 우리를 도왔 발견 google contacts php example입니다 :

https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php

편집 : 여기

도움이 될 또 다른 링크입니다. 의견에서 JSON을 사용하여 연락처의 데이터에 액세스하는 것을보다 명확하게 설명합니다. 당신이 curl_file_get_contents 대신 file_get_contents를 사용하는 경우

http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken; 
$xmlresponse = curl_file_get_contents($url); 

$temp = json_decode($xmlresponse,true); 

foreach($temp['feed']['entry'] as $cnt) { 
    echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address'] . "</br>"; 
} 

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken; 
$xmlresponse = curl_file_get_contents($url); 

$temp = json_decode($xmlresponse,true); 

foreach($temp['feed']['entry'] as $cnt) { 
    echo $cnt['title']['$t'] . " --- " . $cnt['gd$email']['0']['address']; 
    if(isset($cnt['gd$phoneNumber'])) echo " --- " . $cnt['gd$phoneNumber'][0]['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$street'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$street']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$neighborhood'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$neighborhood']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$pobox'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$pobox']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$postcode'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$postcode']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$city'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$city']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$region'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$region']['$t']; 
    if(isset($cnt['gd$structuredPostalAddress'][0]['gd$country'])) echo " --- " . $cnt['gd$structuredPostalAddress'][0]['gd$country']['$t']; 
    echo "</br>"; 
} 
+1

잘 작동 –