2012-11-24 29 views
0

我得到問題,讓我的PHP腳本發佈到博客使用客戶端HTTP身份驗證。發佈到PHP的博主

我已經按照從https://developers.google.com/blogger/docs/1.0/developers_guide_php

這裏的教程是我的代碼:

require_once 'Zend/Loader.php'; 
Zend_Loader::loadClass('Zend_Gdata'); 
Zend_Loader::loadClass('Zend_Gdata_Query'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

$user = 'myuserid'; 
$pass = 'mypassword'; 
$blogUrl = 'myblog.blogspot.com'; 
$service = 'blogger'; 

$gdClient = new Zend_Gdata($client); 
$blogID = getBlogId($gdClient, 'http://'.$blogUrl.'/feeds/posts/default'); 

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, 
     Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, 
     Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); 

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

    $createdPost = $gdClient->insertEntry($entry, $uri); // I think the error is here. 
    $idText = split('-', $createdPost->id->text); 
    $newPostID = $idText[2]; 

    return $newPostID; 
} 

function getBlogId($gdClient, $feed) 
{ 
    $gdClient = new Zend_Gdata($client); 
    $query = new Zend_Gdata_Query($feed); 
    $feed = $gdClient->getFeed($query); 
    preg_match('/blog-([0-9]+)/', $feed->id->text, $match); 
    if (isset($match[1])) 
    { 
     return $match[1]; 
    } 
    return false; 
} 

createPublishedPost($gdClient, $blogID); 

錯誤消息是:

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 401 User does not have permission to create new post' in /home/content/36/9549036/html/[MYUSER]/Zend/Gdata/App.php:714 Stack trace: #0 /home/content/36/9549036/html/[MYUSER]/Zend/Gdata.php(221): Zend_Gdata_App->performHttpRequest('POST', 'https://www.blo...', Array, '<atom:entry xml...', 'application/ato...', NULL) #1 /home/content/36/9549036/html/[MYUSER]/Zend/Gdata/App.php(905): Zend_Gdata->performHttpRequest('POST', 'https://www.blo...', Array, '<atom:entry xml...', 'application/ato...') #2 /home/content/36/9549036/html/[MYUSER]/Zend/Gdata/App.php(980): Zend_Gdata_App->post(Object(Zend_Gdata_Entry), 'https://www.blo...', NULL, NULL, Array) #3 /home/content/36/9549036/html/[MYUSER]/test.php(29): Zend_Gdata_App->insertEntry(Object(Zend_Gdata_Entry), 'https://www.blo...') #4 /home/content/36/9549036/html/[MYUSER]/test.php(49): createPublishedPost(Object(Zend_Gdata), '[MYBLOGID]...') #5 {main} thrown in /home/content/36/9549036/html/[MYUSER]/Zend/Gdata/App.php on line 714 

有沒有人經歷過這個? 是否有任何工作代碼用PHP發佈博客?

+0

您確定它不是驗證問題,即不正確的用戶名或密碼? – Nadh

+0

nah im很確定它不是..無論如何,我似乎已經找到了問題,生病張貼我固定的東西儘快。 – choz

回答

0

我發現了這個問題,它以這種方式工作。

require_once 'Zend/Loader.php'; 
Zend_Loader::loadClass('Zend_Gdata'); 
Zend_Loader::loadClass('Zend_Gdata_Query'); 
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

$user = $_SESSION['BloggerUsername']; 
$pass = $_SESSION['BloggerPassword']; 
$blogUrl = $_SESSION['BloggerUrl']; 
$service = 'blogger'; 

global $gData, $gBlogId; 

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, 
     Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, 
     Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); 

function createPublishedPost($gData, $gBlogId, $myBlogTitle, $myBlogText) 
{ 
    $uri = 'https://www.blogger.com/feeds/' . $gBlogId . '/posts/default'; 
    $entry = $gData->newEntry(); 
    $entry->title = $gData->newTitle($myBlogTitle); 
    $content = $gData->newContent($myBlogText); 
    $content->setType('text'); 
    $entry->content = $content; 
    $entryResult = $gData->insertEntry($entry, $uri); 
    //$idText = split('-', $entryResult->id->text); 
    //$newPostID = $idText[2]; 
    if (!empty($_SERVER['HTTP_REFERER'])) { 
     header("Location: ".$_SERVER['HTTP_REFERER']); 
    } else { 
     header("location:*my_website_link*"); 
    } 
    //return $newPostID; 
} 

function getBlogId($gData, $feed) 
{ 
    $query = new Zend_Gdata_Query($feed); 
    $feed = $gData->getFeed($query); 
    preg_match('/blog-([0-9]+)/', $feed->id->text, $match); 
    if (isset($match[1])) 
    { 
     return $match[1]; 
    } 
    return false; 
} 
$gData = new Zend_Gdata($client); 
$gBlogId = getBlogId($gData, 'http://'.$blogUrl.'/feeds/posts/default'); 
createPublishedPost($gData, $gBlogId, $blogTitle, $blogText); 
+0

這實際上並不能解釋問題所在,也不能幫助其他人解決同樣的問題。任何人都不可能簡單地複製/粘貼代碼的專有示例,而不解釋底層問題以及爲何解決此問題。 – Kato