代码之家  ›  专栏  ›  技术社区  ›  SimpleSpawn

Blogger-设置条目将来的发布日期

  •  1
  • SimpleSpawn  · 技术社区  · 9 年前

    我假设我使用以下代码添加一个新帖子。如何设置公开发布的日期?

    <?php
    /**
     * Zend Framework (http://framework.zend.com/)
     *
     * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
     * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
     * @license   http://framework.zend.com/license/new-bsd New BSD License
     */
    
    namespace Application\Controller;
    set_time_limit (300);
    
    use Zend\View\Model\ViewModel;
    use Zend\Mvc\Controller\AbstractActionController,
        Zend\Console\Request as ConsoleRequest;
    
    use Zend\Http\Request;
    use Zend\Http\Client;
    use Zend\Http\Cookies;
    use Zend\Http\Header;
    use Zend\Stdlib\Parameters;
    use Zend\Json\Json;
    
    class IndexController extends AbstractActionController
    {
        public function indexAction()
        {
    $client = new \Google_Client();
    $redirectUri='http://czystyping.dev/application/index/index';$client->setRedirectUri($redirectUri);
    //$scriptUri = "http://".$_SERVER["HTTP_HOST"];///.$_SERVER['PHP_SELF'];$client->setRedirectUri($scriptUri);
    $client->setAccessType('online'); // default: offline
    $client->setApplicationName('Blogspot');
    //$client->setAuthConfigFile('Blogspot-b4ae4.json');
    $client->setClientId('st.apps.googleusercontent.com');
    $client->setClientSecret('my-secret-string');
    $client->setDeveloperKey('INSERT DEV HERE'); // API key
    $client->setScopes("https://www.googleapis.com/auth/blogger");
    $service = new \Google_Service_Blogger($client);
    
    
    if(isset($authUrl)) {
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
      } else {
       print "<a class='logout' href='?logout'>Logout</a>";
      }
    if (isset($_GET['logout'])) { // logout: destroy token
        unset($_SESSION['token']);
        die('Logged out.');
    }
    if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    
        //var_dump($_GET['code']);
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        //$_SESSION['access_token'] = $client->getAccessToken(); 
    
            // Once the access token is retrieved, you no longer need the
            // authorization code in the URL. Redirect the user to a clean URL.
            //header('Location: '.filter_var($redirectUri, FILTER_SANITIZE_URL));
            //die();
    
    }
    if (isset($_SESSION['token'])) { // extract token from session and configure client
        $token = $_SESSION['token'];
        $client->setAccessToken($token);
    }
    if (!$client->getAccessToken()) { // auth call to google
        $authUrl = $client->createAuthUrl();
        header("Location: ".$authUrl);
        die;
    }
    $blog      = $service->blogs->getByUrl('http://tapetoposty.blogspot.com/');
    $blogName  = $blog->getName();
    $blogUrl   = $blog->getURL();
    $postsObj  = $blog->getPosts();
    $postCount = $postsObj->getTotalItems();
    $posts     = $postsObj->getItems();
    $blogID  = $blog->getID();
    
    $newpost = new \Google_Service_Blogger_Post();
    $newpost->setTitle("2 Example post from zend");
    $newpost->setContent("This is test content 3");
    $newpost->setLabels(array('3d', 'blog z tapetami', 'do', 'hd', 'komputer', 'krajobrazy', 'lato', 'pulpit', 'tapeta', 'tapetoblog', 'tapety', 'tło', 'windows', 'wodospad', 'zdjęcia', 'zwierzęta', 'świąteczne'));
    //$newpost->setUrl('example-url');
    $newpost->setPublished('true'); //make Google_Service_Exception in /vendor/google/apiclient/src/Google/Http/REST.php:79 Message: Error calling POST https://www.googleapis.com/blogger/v3/blogs/1534947238029354603/posts?key=INSERT+DEV+HERE: (400) Invalid value for: Invalid format: "true"
    
    
    //$newpost->setImages($images);
    //$newpost->setTitleLink('title-link-example'); //make Google_Service_Exception
    //  $newpost->setCustomMetaData($customMetaData); //untested
    $post = $service->posts->insert($blogID, $newpost, array());
    
    
    print_r($post);
    

    如有任何帮助,我将不胜感激。

    编辑1: 使用此代码

    $now = new \DateTime('NOW');
    $day = $now->modify('+17 day');
    //var_dump($day);
    $newpost->setPublished($day->date);
    

    导致错误ok,帖子立即发布。也许我需要使用setCustomMetaData($customMetaData)才能正常工作?但我不知道如何构建$customMetaData(正如我在GoogledocsOAuth游乐场上读到的,这是作者和发布日期)

    编辑2:谢谢@abraham纠正我的错误,因为我没有使用翻译。

    1 回复  |  直到 9 年前
        1
  •  0
  •   ankhzet    9 年前

    如中所述 javadoc 对于 Google_Service_Blogger_Posts_Resource::publish() 方法:

    @opt_param string publishDate计划的可选日期和时间 博客的发布。如果没有给定publishDate参数,则post为 在以前保存的计划日期(如果存在)发布,或 当前时间。如果给出了未来的日期,该职位将被安排为 出版。

    所以 Google_Service_Blogger_Post::setPublished($pubDate) 方法需要字符串日期参数 RFC 3339 格式和适当的发布日期的方式如下:

    $date = new DateTime('+17 day'); 
    $newpost->setPublished($date->format(DateTime::RFC3339));