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



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

<?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 ApplicationController;
set_time_limit (300);
use ZendViewModelViewModel;
use ZendMvcControllerAbstractActionController,
    ZendConsoleRequest as ConsoleRequest;
use ZendHttpRequest;
use ZendHttpClient;
use ZendHttpCookies;
use ZendHttpHeader;
use ZendStdlibParameters;
use ZendJsonJson;
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);
如有任何帮助,我将不胜感激。

Edit1:使用以下代码

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

导致错误ok,文章已立即发布。也许我需要使用esetcustommetadata ($customMetaData)来正常工作?但是我不知道如何构建$customMetaData(正如我在google docs OAuth playground上读到的,这集作者以及出版日期)

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

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

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

因此,Google_Service_Blogger_Post::setPublished($pubDate)方法期望字符串日期参数具有RFC 3339格式,正确的发布方式是这样的:

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

相关内容

  • 没有找到相关文章