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

时间:2015-01-07 06:39:02

标签: php arrays xml google-api

我正在尝试在我的代码中导入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。

1 个答案:

答案 0 :(得分:1)

在最后一个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)。因此,要在创建数组时获得相同的结果,可以通过将(字符串)放在变量名称前面来将其强制转换为字符串。

编辑:

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

$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;