Google使用PHP联系API XML解析
我有一个来自GoogleContactsAPI的xml提要,但是我在阅读更新时遇到了困难:如果您使用的是名称空间和xpath,那么可能是SimpleXML 如果您在访问元素时遇到问题,请尝试将其括在Google使用PHP联系API XML解析,php,gdata,google-data-api,google-contacts-api,Php,Gdata,Google Data Api,Google Contacts Api,我有一个来自GoogleContactsAPI的xml提要,但是我在阅读更新时遇到了困难:如果您使用的是名称空间和xpath,那么可能是SimpleXML 如果您在访问元素时遇到问题,请尝试将其括在{}尖括号内,这可能会有所帮助:$xml->{gd:…} 如果我没记错的话,SimpleXML对XML名称空间没有/有限的支持。是的,所以拿着它吧 第一次开始: $dom = new DomDocument; $dom->loadXML($google_contacts); ... 或者仅对
{}
尖括号内,这可能会有所帮助:$xml->{gd:…}
如果我没记错的话,SimpleXML对XML名称空间没有/有限的支持。是的,所以拿着它吧 第一次开始:
$dom = new DomDocument;
$dom->loadXML($google_contacts);
...
或者仅对SimpleXML中难以完成的部分使用DomDocument:
$domElement = dom_import_simplexml($simpleXmlNode);
您可以使用xml_解析读取“gd:”标记。它返回数组中的所有结果,因为xml字符串不由您共享,所以您需要编写进一步的逻辑来从数组中获取数据
<?php
$content = $google_contacts;
$parser = xml_parser_create();
xml_parse_into_struct($parser, $content, $data, $index);
xml_parser_free($parser);
print"<pre>";
print_r($data);
/*foreach($data as $vals )
{
//write your code to get result from array
}*/
?>
我遇到了同样的问题,无法访问
我做了一些事情,使它更容易使用,就是通过在请求字符串中添加alt=json
将gdata作为json返回
我的完整查询字符串如下所示:
然后就是常规的json。我会继续使用另一个XML解析器。将某些内容与当前的xml to object parser结合在一起可能最终会造成一些问题。取而代之的是DomDocument,它支持xml名称空间。请显示一些代码,尝试在其中访问这些元素。阅读不是特别的意思是什么?
<?php
$content = $google_contacts;
$parser = xml_parser_create();
xml_parse_into_struct($parser, $content, $data, $index);
xml_parser_free($parser);
print"<pre>";
print_r($data);
/*foreach($data as $vals )
{
//write your code to get result from array
}*/
?>
$search=array('<gd:','</gd:');
$replace=array('<gd','</gd');
/*Create json object from $google_contacts(atom xml string)
by replacing "gd:" to "gd"*/
$response = json_encode(str_replace($search, $replace,$google_contacts));
/*Create Array from $response*/
$contactsList = json_decode($response, true);
print "<pre>" . print_r($contactsList['entry'], true);
<? xml version = '1.0' encoding = 'UTF-8' ?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
...
<entry>
<id>http://www.google.com/m8/feeds/contacts/xxxxxxxx%40gmail.com/base/0</id>
<updated>2012-09-23T01:54:56.646Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Will Jordan</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSXN'/>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0'/>
<link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0'/>
<link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1348365xxxxxx'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='yyy@gmail.com' primary='true'/>
<gd:email rel='http://schemas.google.com/g/2005#home' address='zzz@yahoo.com'/>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'>8888888888</gd:phoneNumber>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'>9999999999</gd:phoneNumber>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'>5555555555</gd:phoneNumber>
<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/4fde39d984'/>
<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/xxxxxxx%40gmail.com/base/3f5dc5d3be0'/>
</entry>
</feed>
array(
[id] => http://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/base/0
[updated] => 2013-08-27T19:33:22.431Z
[category] => Array(
[@attributes] => Array(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Will Jordan
[link] => Array(
[0] => Array(
[@attributes] => Array(
[rel] => http://schemas.google.com/contacts/2008/rel#edit-photo
[type] => image/ *
[href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0/eAEUjCL-B1cSX
)
)
[1] => Array(
[@attributes] => Array(
[rel] => http://schemas.google.com/contacts/2008/rel#photo
[type] => image/ *
[href] => https://www.google.com/m8/feeds/photos/media/xxxxxxx%40gmail.com/0
)
)
[2] => Array(
[@attributes] => Array(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0
)
)
[3] => Array(
[@attributes] => Array(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/xxxxxxx%40gmail.com/full/0/1377632
)
)
)
[gdemail] => Array(
[0] => Array(
[@attributes] => Array(
[rel] => http://schemas.google.com/g/2005#other
[address] => yyy@gmail.com
[primary] => true
)
)
[1] => Array(
[@attributes] => Array(
[rel] => http://schemas.google.com/g/2005#home
[address] => zzz@yahoo.com
)
)
)
[gdphoneNumber] => Array(
[0] => 8888888888
[1] => 9999999999
[2] => 5555555555
)
)
https://www.google.com/m8/feeds/contacts/{userEmail}/full?oauth_token=token&max-results=9999999&alt=json');