Blogger post using PHP API

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
session_start();
require_once dirname(__FILE__) . '/GoogleClientApi/src/Google/autoload.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Client.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Service.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Model.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Collection.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Config.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Service/Resource.php';
require_once dirname(__FILE__).'/GoogleClientApi/src/Google/Service/Blogger.php';
//echo '<pre>';
$scriptUri = "<redirect url>";

$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('<appName>'); //name of the application
$client->setClientId('<clientID>'); //insert your client id
$client->setClientSecret('<clientsecret>'); //insert your client secret
$client->setRedirectUri($scriptUri); //redirects to same url
$client->setDeveloperKey('<developerkey>'); // API key (at bottom of page)
$client->setScopes(array('https://www.googleapis.com/auth/blogger')); //since we are going to use blogger services


if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
header("Location: ".$scriptUri);
}

if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
header("Location: bloglist.php");
}

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;
}
if(isset($_POST['submit'])){
$blogger = new Google_Service_Blogger($client);
//you can get the data about the blog by getByUrl
$blog = $blogger->blogs->getByUrl(array('url'=>'<blogurl>'));

//creates a post object
  $mypost = new Google_Service_Blogger_Post();
  $mypost->setTitle($_POST['title']);
  $mypost->setContent($_POST['desc']);
  //echo $blog->getId();
$data = $blogger->posts->insert($blog->getId(), $mypost);
header("Location: bloglist.php");
}else if(isset($_GET['delete']) && isset($_GET['postid'])){
$blogger = new Google_Service_Blogger($client);
$blog = $blogger->blogs->getByUrl(array('url'=>'<blogurl>'));
$data = $blogger->posts->delete($blog->getId(), $_GET['postid']);
header("Location: bloglist.php");

}else if(isset($_POST['update'])){
$blogger = new Google_Service_Blogger($client);
$blog = $blogger->blogs->getByUrl(array('url'=>'<blogurl>'));
$mypost = new Google_Service_Blogger_Post();
$mypost->setTitle($_POST['title']);
$mypost->setContent($_POST['desc']);
$blogger->posts->update($blog->getId(), $_GET['postid'], $mypost);
header("Location: bloglist.php");

}else if(isset($_GET['edit']) && isset($_GET['postid'])){
$blogger = new Google_Service_Blogger($client);
$blog = $blogger->blogs->getByUrl(array('url'=>'<blogurl>'));
$singlepost = $blogger->posts->get($blog->getId(), $_GET['postid']);
}

?>
<form method="post">
<table>

<tr>
<td>Title</td>
<td>
<input type="text" name="title" value="<?php echo isset($singlepost->title) ? $singlepost->title : '';  ?>">
<?php if(isset($singlepost->id)){ ?>
<input type="hidden" name="postid" value="<?php echo $singlepost->id; ?>">
<?php } ?>
</td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="desc"><?php echo isset($singlepost->content) ? $singlepost->content : '';  ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="<?php $val = isset($singlepost->id) ? 'update' : 'submit' ; echo $val; ?>" value="<?php echo $val ; ?>"></td>
</tr>

</table>
</form>

Comments

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging...
    PHP developers in chennai | PHP developers in India

    ReplyDelete

  2. Thanks for sharing this niche useful informative post to our knowledge.
    brochure designers in chennai | brochure design company in chennai

    ReplyDelete
  3. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Best Python training Institute in chennai

    ReplyDelete
  4. A debt of gratitude is in order for your educational post!!! Subsequent to finishing my graduation, I am confounded whether to pick website composition as my profession. Your article helped me to settle on a correct decision.
    MBA Talks | Article Submission sites

    ReplyDelete
  5. I think this was one of the most interesting content I have read today. Please keep posting.
    PHP Training in Chennai
    PHP Course in Chennai

    ReplyDelete
  6. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    java training center in chennai
    java institutes in chennai

    ReplyDelete
  7. Google account. I look forward to fresh updates and will talk about this blog with my Facebook group. Chat soon!
    fire and safety course in chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Thank for the post bro, it helped me a lot. Can you show me the bloglist.php file too?

    ReplyDelete
  10. Very informative post and minimal design. We do provide minimal designs and web development services. Checkout for In budget IT services:
    Web Development | WordPress Development | Web Design | Digital Marketing | Portfolio

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well. great work
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  15. Thanks for sharing such valuable information to us, really informative. keep going expecting for from you.

    Regards..
    Dinesh

    PHP Training in chennai

    ReplyDelete
  16. To be honest, I don’t know how you manage to do such a good job every single time. Very well done!
    php development company

    ReplyDelete
  17. I went through your blog and it’s totally awesome.this was very for me .

    ReplyDelete
  18. Great Post. Very informative. Keep Sharing!!

    Apply Now for PHP Training Classes in Noida

    For more details about the course fee, duration, classes, certification, and placement call our expert at 70-70-90-50-90

    ReplyDelete

Post a Comment

Popular posts from this blog

Pin payment laravel