Create an array from SimpleXMLElement answer in php
I am trying to import google contacts into my code. I have imported contacts successfully, but my problem is that I have to create an array of imported email addresses and I need to transfer it to another page. But when I try to create an array, I get an array containing SimpleXMLElement Object
. Here is my code:
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
?>
My result:
Array ( [0] => Array ( [0] => SimpleXMLElement Object ( [0] => email-address1 ) ) [1] => Array ( [0] => SimpleXMLElement Object ( [0] => email-address2 ) ) )
I need to do the following:
Array ( ([0] => email-address1 ) ( [1] => email-address2 ) )
Can anyone suggest me how to do this. I've been stuck with this for days. thanks in advance
Edit
XML response:
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 ) ) )
I need to separate emailaddress
from a php array response.!
Edit 2
here $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
is the mail ID from which the contacts were imported.
source to share
On the first line of the final foreach loop, you can try:
$var=(string)$title->attributes()->address;
or
$var=(string)$title->attributes()->address[0];
Which is some code from http://php.net/manual/en/simplexml.examples-basic.php#116435
I think the echo statement that correctly lists email addresses implicitly calls the SimpleXMLElement :: _ toString (void) method ( http://php.net/manual/en/simplexmlelement.tostring.php ). So, to get the same result when creating an array, you can force it to be a string by putting (string) in front of the variable name.
EDIT:
You can also try this where you add to the array:
$gc[] = $var;
instead
$gc[] = array($var);
Since the second method creates a new array object and adds $ var to it, the assignment statement adds the entire array to $ gc. This explains why SimpleXMLObject was itself in the array. The first way ('$ gc [] = $ var;') will add a new element to the $ gc array. Or you can initialize $ gc before the foreach loop with:
$gc = array();
And then inside the foreach loop, use:
array_push($gc, $var);
You will need to make both of the suggested changes to get the desired result, so:
foreach ($results as $title) {
$var=(string)$title->attributes()->address;
$gc[] = $var;
source to share