2011-04-27 116 views
0

我想使用下面的代碼發佈到我的博客帳戶,但它似乎不工作。我是這個新手,我錯過了什麼?謝謝... `使用Zend Gdata發佈到博客

<?php 

$user = '[email protected]'; 
$pass = 'password'; 

// I have to admit, I would normally use the autoloader 
require_once 'Zend/Loader.php'; 
Zend_Loader::loadClass('Zend_Gdata'); 
Zend_Loader::loadClass('Zend_Gdata_Query'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
Zend_Loader::loadClass('Zend_Gdata_Feed'); 

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null, 
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, 
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); 
$gdClient = new Zend_Gdata($client); 

function createPublishedPost() 
{ 
    $title='Hello, world!'; 
    $content='I am blogging on the internet.'; 
    $blogID= "4419618066922958909"; 
    $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default'; 
    $entry = $gdClient->newEntry(); 
    $entry->title = $gdClient->newTitle($title); 
    $entry->content = $gdClient->newContent($content); 
    $entry->content->setType('text'); 

    $createdPost = $gdClient->insertEntry($entry, $uri); 
    $idText = split('-', $createdPost->id->text); 
    $newPostID = $idText[2]; 

    return $newPostID; 
} 

$ret = createPublishedPost(); 
echo $ret; 
?> 

`

回答

0

這是我們很難知道哪裏出了問題不知道什麼比您正在使用哪些代碼。所以首先你需要:

  • 檢查認證拋出任何異常
  • 檢查是否insertEntry拋出任何異常
  • 檢查什麼insertEntry返回

參見代碼中的註釋看我已經改變了:

<?php 

$user = '[email protected]'; 
$pass = 'password'; 

// I have to admit, I would normally use the autoloader 
require_once 'Zend/Loader.php'; 
Zend_Loader::loadClass('Zend_Gdata'); 
Zend_Loader::loadClass('Zend_Gdata_Query'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
Zend_Loader::loadClass('Zend_Gdata_Feed'); 

//Checks if getHttpClient throws any exceptions 
try { 
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'blogger', null, 
    Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, 
    Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); 
} catch (Zend_Gdata_App_AuthException $ae) { 
    echo 'Problem authenticating: ' . $ae->exception() . "\n"; 
} 

$gdClient = new Zend_Gdata($client); 

function createPublishedPost() 
{ 
    $title='Hello, world!'; 
    $content='I am blogging on the internet.'; 
    $blogID= "4419618066922958909"; 
    $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default'; 
    $entry = $gdClient->newEntry(); 
    $entry->title = $gdClient->newTitle($title); 
    $entry->content = $gdClient->newContent($content); 
    $entry->content->setType('text'); 

    //Checks if insertEntry throws any exceptions 
    try{ 
     $createdPost = $gdClient->insertEntry($entry, $uri); 
    } catch (Zend_Gdata_App_AuthException $ae) { 
     echo 'Problem authenticating: ' . $ae->exception() . "\n"; 
    } 

    var_dump($createdPost); //Will print the variable and what type it is 
    $idText = split('-', $createdPost->id->text); 
    $newPostID = $idText[2]; 

    return $newPostID; 
} 

$ret = createPublishedPost(); 
echo $ret; 
?> 
+0

嘗試了你的代碼...我得到錯誤500 ..我已經測試了單獨的身份驗證,它工作正常..與創建職位的功能我猜 – 2011-04-27 08:13:41

+0

另一件好事:檢查發送到insertEntry的uri。 – rzetterberg 2011-04-27 08:21:32

0

明白了一些實驗...感動$gdClient = new Zend_Gdata($client);裏面的函數。以前它不是全局聲明的。