2011-06-30 25 views
7

Điều tôi đang cố gắng hiện tại chỉ là thuộc tính của gd: email là "address =" chỉ không có gì khác vào lúc này. Mà tôi có thể nhận được phần xml, heck tôi thậm chí có thể nhận được bất kỳ suy nghĩ cho mỗi nói miễn là thẻ bên trong của nó như thế nhưng để có được các thuộc tính của bất kỳ một trong những như trong trường hợp của tôi .. Tôi hoàn toàn nhầm lẫn trên. Tôi đã từng biết làm thế nào để làm điều đó nhưng nó đã được rất lâu kể từ khi tôi đã làm bất cứ điều gì đó không phải là đơn giản cho việc sử dụng XML. vì vậy tôi đã thực hiện xong điều sai lầm tự của riêng tôi lên ..PHP GMAIL Liên hệ Phân tích cú pháp XML với DOMDocument và cURL

<?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'> 
     <id>[email protected]</id> 
     <updated>2011-06-30T00:07:48.706Z</updated> 
     <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
     <title type='text'>Taco Bells's Contacts</title> 
     <link rel='alternate' type='text/html' href='http://www.google.com/'/> 
     <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/> 
     <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/> 
     <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/batch'/> 
     <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?max-results=5'/> 
     <link rel='next' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?start-index=6&max-results=5'/> 
     <author> 
      <name>Taco Bell</name> 
      <email>[email protected]</email> 
     </author> 
     <generator version='1.0' uri='http://www.google.com/mate/feeds'>Contacts</generator> 
     <openSearch:totalResults>90</openSearch:totalResults> 
     <openSearch:startIndex>1</openSearch:startIndex> 
     <openSearch:itemsPerPage>5</openSearch:itemsPerPage> 
     <entry> 
      <id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/0</id> 
      <updated>2010-01-27T00:11:57.430Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <title type='text'>Taco B</title> 
      <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0/8_XL_JoAPon1k7SNixI2iA'/> 
      <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0'/> 
      <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0'/> 
      <link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0/1264551117430000'/> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry> 
     <entry> 
      <id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/1</id> 
      <updated>2007-08-01T18:02:04.410Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <title type='text'></title> 
      <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/1/1B2M2Y8AsgTpgAmY7PhCfg'/> 
      <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1'/> 
      <link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1/1185991324410001'/> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry> 
     <entry> 
      <id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/2</id> 
      <updated>2010-01-27T00:11:57.430Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <title type='text'>Steve Sattler</title> 
      <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/2/1B2M2Y8AsgTpgAmY7PhCfg'/> 
      <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2'/> 
      <link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2/1264551117430000'/> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry> 
     <entry> 
      <id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/3</id> 
      <updated>2010-01-27T00:11:57.430Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <title type='text'>Michael Montana</title> 
      <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/3/1B2M2Y8AsgTpgAmY7PhCfg'/> 
      <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3'/> 
      <link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3/1264551117430000'/> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry> 
     <entry> 
      <id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/4</id> 
      <updated>2007-08-01T18:02:04.410Z</updated> 
      <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/> 
      <title type='text'></title> 
      <link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/4/1B2M2Y8AsgTpgAmY7PhCfg'/> 
      <link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4'/> 
      <link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4/1185991324410001'/> 
      <gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/> 
     </entry> 
    </feed> 

với mã trông giống như (đây là toàn bộ kịch bản (hiện tại))

$user = "[email protected]"; 
$password = "xxxxxxxxxxxx"; 

// ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html 

// step 1: login 
$login_url = "https://www.google.com/accounts/ClientLogin"; 
$fields = array(
    'Email' => $user, 
    'Passwd' => $password, 
    'service' => 'cp', // <== contact list service code 
    'source' => 'test-google-contact-grabber', 
    'accountType' => 'GOOGLE', 
); 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,$login_url); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl); 

$returns = array(); 

foreach (explode("\n",$result) as $line) 
{ 
    $line = trim($line); 
    if (!$line) continue; 
    list($k,$v) = explode("=",$line,2); 

    $returns[$k] = $v; 
} 

curl_close($curl); 

// step 2: grab the contact list 
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?&max-results=5"; 

$header = array(
    'Authorization: GoogleLogin auth=' . $returns['Auth'], 
); 

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $feed_url); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

$result = curl_exec($curl); 
curl_close($curl); 

$doc = new DOMDocument(); 
$doc->load($result); 
$arrFeeds = array(); 
foreach ($doc->getElementsByTagName('entry') as $node) { 
    $monkey = $node->getAttribute('{gd:email}'); 
    $itemRSS = array($monkey->nodeValue); 
array_push($arrFeeds, $itemRSS); 
} 
array_unique($arrFeeds); 

Trả lời

16

Có một cái nhìn tại DOMDocumentDOMXPath. Để nhận các nút là một phần của một không gian tên cụ thể, như nút email là một phần của không gian tên gd, bạn cần phải đăng ký không gian tên với đối tượng DOMXPath bằng cách sử dụng DOMXPath::registerNamespace(). URI không gian tên thường có thể được tìm thấy ở đầu tài liệu XML.

Ví dụ:

$doc = new DOMDocument; 
$doc->recover = true; 
$doc->loadXML($result); 

$xpath = new DOMXPath($doc); 
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005'); 

$emails = $xpath->query('//gd:email'); 

foreach ($emails as $email) 
{ 
    echo $email->getAttribute('address'); 

    // To get the title. 
    // This could also be done using XPath. 
    // You can also use ->nodeValue instead of ->textContent. 
    echo $email->parentNode->getElementsByTagName('title')->item(0)->textContent; 
} 

Trong ví dụ trên, $result là kết quả của $result = curl_exec($ch);.

+0

Thật tuyệt vời! Tôi không nghĩ rằng tôi sẽ đi đến kết luận đó một mình trong một thời gian ngắn. Tò mò tuy nhiên. "// gd: email" phần "//" có nghĩa là gì? Giống như ý nghĩa của điều đó là gì? Sau đó, "$ xpath-> registerNamespace ('gd', 'http://schemas.google.com/g/2005');" là có anyway để không có http: // ...... một phần nguyên nhân im nghĩ gì nếu theres bao giờ một cái gì đó khác .. – chris

+1

@chris - // chỉ có nghĩa là: "Chọn các nút trong tài liệu từ nút hiện tại khớp với vùng chọn bất kể chúng ở đâu ". Để biết thêm thông tin, xem [XPath Syntax tại W3Schools.com] (http://www.w3schools.com/xpath/xpath_syntax.asp). URI vùng tên luôn bắt đầu bằng lược đồ (tức là http), do đó bạn phải giữ trong đó. –

+0

Ngay trước khi bạn trả lời câu hỏi này. Tôi nhận ra tôi cũng muốn có được "danh hiệu" của mỗi "mục" để tôi có thể đặt tên của người đó bằng email. Tôi đã không nhận ra và đề cập đến điều đó trong bài viết ban đầu của mình. Có phương tiện nào tôi có thể kết hợp điều đó vào điều này mà bạn đã trình bày ở trên hay ý tưởng đó phải được viết lại thành một ngữ cảnh khác để quản lý điều đó không? – chris

0
$url = 'https://www.google.com/m8/feeds/contacts//full?max-results='.$max_results.'&oauth_token='.$accesstoken.'&alt=json&updated-min=2007-03-16T00:00:00'; 
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)'; 
//The URL to fetch. This can also be set when initializing a session with curl_init(). 
curl_setopt($curl,CURLOPT_URL,$url);  
curl_setopt($curl, CURLOPT_HTTPHEADER,array('GData-Version: 2.0')); 
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.  
curl_setopt($curl,CURLOPT_HTTPGET,true); 
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request. 
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header. 
//curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect. 
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute. 
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate. 

$contents = curl_exec($curl); 

curl_close($curl); 
return $contents; 
} 
Các vấn đề liên quan