2

I really need your help with this matter. I'm looking for a solution for about 3 months now, but really the Blogger API is not easy to deal with because Blogger don't even provide examples.

I can create and publish new posts with PHP script and I have done all things, but I can't set the Post's description, permalink, or even making the new post as draft.

The following is a piece of my code that creating the post.

<?php

    $mypost = new Google_Post();
    $mypost->setTitle('My Post Title');
    $mypost->setContent('This is My Content');
    $mypost->setLabels( array( 'News','Weather', 'Media' ) );
    $mypost->setCustomMetaData('My_CUSTOM_META_DATA' . time()); // Nothing changed
    $mypost->setcustomMetaData('This is the description for you');  //Nothing Changed
    $mypost->setDescription('New Description');   // Nothing Changed
    $mypost->setUrl('testseturl');   // Nothing Changed
    $mypost->setPublished('2021-08-27T23:07:00-07:00');  // Worked as Schedule post

    $data = $blogger->posts->insert('My BlogID', $mypost); 

    echo "<pre>";
    var_dump($data);
    echo "</pre>";
?>

As you can see I can't set the permalink and I tried several thing such as adding the full URL, and also adding only the custom permalink text + html, but I failed.

I tried also the description several times, but every time I found the description's post empty.

Also I can' set the post as Draft and I have to do this manually from the blog itself.

Blogger doesn't provide any help docs for PHP, and the new Beta client library on github is for all Google products and I wasn't able to use it. I use the library Google API PHP Client 0.6.7 found here although it's deprecated.

The only topic I found in this blog, and it's the same code that I use, but he didn't mentioned anything about permalink, draft, or description.

Please help me as you can.

Thanks.

1 Answers1

2

permalink

Unfortunately there is no way to set custom permalink to the posts using Blogger api, even the official "Try this API" tool don't have this feature, your code is fine it's just Blogger don't support it.

custom description

I don't think there is a way to add custom description as well, setDescription is not a valid method, you can check all the supported methods here

draft post

to create a post draft you can do it like this

$optParams = array('isDraft' => true);
$data = $blogger->posts->insert('My BlogID', $mypost, $optParams); 
Bouh
  • 1,303
  • 2
  • 10
  • 24
  • 1- Thanks for your reply. I didn't know there is now way to set the custom permalink using Blogger API, but what setUrl() actually do. 2- About the setDescription() it's exist as function on the library I have. In this path "../GoogleClientAPI/src/contrib/Google_BloggerService.php" I found the following code: public function setDescription( $description) { $this->description = $description; } public function getDescription() { return $this->description; } – Ahmed Sheta Aug 08 '20 at 13:56
  • 3- I tried your code , but I get this error: Fatal error: Uncaught exception 'Google_Exception' with message '(insert) unknown parameter: 'isDraft'' in ...\Project Path\GoogleClientApi\src\service\Google_ServiceResource.php:111 Stack trace: #0 ...\Project Path\GoogleClientApi\src\contrib\Google_BloggerService.php(292): Google_ServiceResource->__call('insert', Array) #1 ...\Project Path\\testapi.php(978): Google_PostsServiceResource->insert('BlogID...', Object(Google_Post), Array) #2 {main} thrown in ...\Project Path\GoogleClientApi\src\service\Google_ServiceResource.php on line 111 – Ahmed Sheta Aug 08 '20 at 13:57
  • `setUrl` should be the method that sets the custom url but it's not working! the errors you are getting because you are using an old package which is [deprecated](https://code.google.com/archive/p/google-api-php-client/downloads), you can find the latest versions [here](https://github.com/googleapis/google-api-php-client/releases) – Bouh Aug 08 '20 at 14:30
  • Thanks Bouh for your Support. One last question if you don't mind. Can I use the same code with the new package? or I have to know the methods name of the new package from somewhere?. Thanks in advance. – Ahmed Sheta Aug 08 '20 at 21:05
  • you will need to make some changes because the two versions are bit different – Bouh Aug 08 '20 at 22:59
  • Many Thanks Bouh for you helpful replies. I appreciate your help. – Ahmed Sheta Aug 09 '20 at 02:13
  • 1
    I want to let you know that I have modified my code to use the new version and now I can set the post as draft. Thanks again Bouh. – Ahmed Sheta Aug 09 '20 at 03:32