Blogger - 将条目的发布日期设置为将来
Blogger - Set the date of publication of the entry in the future
我假设我使用以下代码添加新的 post。如何设置日期publicpost?
<?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,post马上发布。也许我需要使用 setCustomMetaData($customMetaData) 才能正常工作?但我不知道如何构建 $customMetaData(正如我在 google docs OAuth playground 上读到的这组作者以及 public 创作日期)
Edit2:感谢@abraham 纠正我的错误,因为我没有使用翻译器。
如 javadoc
中所述 Google_Service_Blogger_Posts_Resource::publish()
方法:
@opt_param string publishDate Optional date and time to schedule the
publishing of the Blog. If no publishDate parameter is given, the post is
either published at the a previously saved schedule date (if present), or the
current time. If a future date is given, the post will be scheduled to be
published.
因此,Google_Service_Blogger_Post::setPublished($pubDate)
方法需要具有 RFC 3339
格式的字符串日期参数,并且使用所需日期发布的正确方式如下:
$date = new DateTime('+17 day');
$newpost->setPublished($date->format(DateTime::RFC3339));
我假设我使用以下代码添加新的 post。如何设置日期publicpost?
<?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,post马上发布。也许我需要使用 setCustomMetaData($customMetaData) 才能正常工作?但我不知道如何构建 $customMetaData(正如我在 google docs OAuth playground 上读到的这组作者以及 public 创作日期)
Edit2:感谢@abraham 纠正我的错误,因为我没有使用翻译器。
如 javadoc
中所述 Google_Service_Blogger_Posts_Resource::publish()
方法:
@opt_param string publishDate Optional date and time to schedule the publishing of the Blog. If no publishDate parameter is given, the post is either published at the a previously saved schedule date (if present), or the current time. If a future date is given, the post will be scheduled to be published.
因此,Google_Service_Blogger_Post::setPublished($pubDate)
方法需要具有 RFC 3339
格式的字符串日期参数,并且使用所需日期发布的正确方式如下:
$date = new DateTime('+17 day');
$newpost->setPublished($date->format(DateTime::RFC3339));