从 php 中的 SimpleXMLElement 响应创建一个数组


Create an array from SimpleXMLElement response in php

我正在尝试在我的代码中导入Google联系人。我已经成功导入了联系人,但我的问题是,我必须创建一个导入的电子邮件地址数组,我需要将其传递到另一个页面。但是当我尝试创建一个数组时,我得到一个包含SimpleXMLElement Object的数组。这是我的代码:

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] => Array ( [0] => SimpleXMLElement Object ( [0] => email-address1 ) ) [1] => Array ( [0] => SimpleXMLElement Object ( [0] => email-address2 ) ) )

我需要这样:

Array ( ([0] =>  email-address1 )  ( [1] => 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 ) ) )

我必须将emailaddress与对 php 数组的响应分开。!

编辑 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。

在最后一个 foreach 循环的第一行中,您可以尝试:

$var=(string)$title->attributes()->address;

或:

$var=(string)$title->attributes()->address[0];

这是来自 http://php.net/manual/en/simplexml.examples-basic.php#116435 的一些代码

我认为正确列出电子邮件地址的 echo 语句隐式调用了 SimpleXMLElement::_toString(void) 方法 (http://php.net/manual/en/simplexmlelement.tostring.php)。因此,要在创建数组时获得相同的结果,您可以通过在变量名称前面放置 (string) 来将其强制为字符串。

编辑:

您也可以在添加到数组的地方尝试此操作:

$gc[] = $var;

而不是

$gc[] = array($var);

由于第二种方法创建一个新的数组对象并向其添加$var,因此赋值运算符会将整个数组添加到$gc。这可以解释为什么 SimpleXMLObject 本身在数组中。第一种方法 ('$gc[] = $var;') 将向数组$gc添加一个新元素。或者,您可以使用以下命令在 foreach 循环之前初始化$gc:

$gc = array();

然后在foreach循环中使用:

array_push($gc, $var);

您需要执行两个建议的更改才能获得所需的结果,因此:

foreach ($results as $title) {
    $var=(string)$title->attributes()->address;
    $gc[] = $var;