코드에서 Google 연락처를 가져 오려고합니다. 연락처를 성공적으로 가져 왔지만 문제는 가져온 이메일 주소의 배열을 만들어야하고 다른 페이지로 전달해야한다는 것입니다. 그러나 배열을 만들려고하면 SimpleXMLElement Object
을 포함하는 배열을 얻게됩니다. 여기 내 코드입니다 :PHP에서 SimpleXMLElement 응답으로 배열 만들기
oauth.php :
<?php
$client_id='my-cient-id';
$client_secret='my-client-secret';
$redirect_uri='my-redirect-uri';
$max_results = 100;
$auth_code = $_GET["code"];
function curl_file_get_contents($url)
{
$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);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken;
$xmlresponse = curl_file_get_contents($url);
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) //At times you get Authorization error from Google.
{
echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>";
exit();
}
echo "<h3>Email Addresses:</h3>";
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
foreach ($result as $title) {
$var=$title->attributes()->address;
echo $var . "<br>"; //here imported contacts is listing properly
$gc[]=array($var); // creates my contacts array
}
print_r($gc); //printing the created array
?>
내 결과는 :
이Array (([0] => email-address1) ([1] => email-address2))
사람이 저를 제안 할 수 있습니다 :
Array ([0] => Array ([0] => SimpleXMLElement Object ([0] => email-address1)) [1] => Array ([0] => SimpleXMLElement Object ([0] => email-address2)))
내가 이런 식으로 얻을 필요 어떻게하는지. 나는 여러 날 동안 이것에 갇혀있다. 사전
감사편집
XML 응답 :
Array ([0] => SimpleXMLElement Object ([@attributes] => Array ([rel] => http://schemas.google.com/g/2005#other [address] => emailaddress1 [primary] => true)) [1] => SimpleXMLElement Object ([@attributes] => Array ([rel] => http://schemas.google.com/g/2005#other [address] => emailaddress2 [primary] => true)))
나는 PHP 배열에 대한 응답에서 emailaddress
를 분리해야합니다.! 여기
편집 2
는 $xmlresponse
입니다 :
email-id 2015-01-07T09:03:23.311Z profile-name email-id Contacts 2 1 100 http://www.google.com/m8/feeds/contacts/email-id/base/6cc9fe478fd427bb 2014-12-30T04:54:29.902Z
email-id
는 접점을 가져 오는 메일 ID입니다.
$var=(string)$title->attributes()->address;
나 :
xml 응답을 게시 할 수 있습니까? –