<?php
$url = "https://accounts.google.com/o/oauth2/auth";
$params = array(
"response_type" => "code",
"client_id" => "XXXXXXXXXX.apps.googleusercontent.com",
"redirect_uri" => "http://localhost/phpBlogger/oauth2callback.php",
"scope" => "https://www.googleapis.com/auth/blogger"); // Request Blogger authentication
$request_to = $url.'?'.http_build_query($params);
header("Location: ".$request_to);
?>
if(isset($_GET['code'])) {
// Get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$blogId = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$params = array(
"code" => $code,
"client_id" => urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"),
"client_secret" => urlencode("XXXXXXXXXXXXXXXXXXXXXXXXX"),
"redirect_uri" => urlencode("http://localhost/phpBlogger/oauth2callback.php"),
"grant_type" => urlencode("authorization_code")
);
// HTTP query builder
foreach($params as $key=>$value) {
$fields .= $key.'='.$value.'&';
}
rtrim($fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
//echo curl_errno($ch);
$response = json_decode($data);
$accessToken = $response->access_token;
}
$postTitle = 'post test';
$postContent = 'just another test.';
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogId.'/posts/';
$body = ' { "kind": "blogger#post", "blog": {"id": "'.$blogId.'"}, "title": "'.$postTitle.'", "content": "'.$postContent.'" }';
$headerQuery = array();
$headerQuery[] = 'Authorization: OAuth '.$accessToken;
$headerQuery[] = 'Content-Length: '.strlen($body);
$headerQuery[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerQuery);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$data = curl_exec($ch);
/*var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
echo "<br><br><br>".$data;*/
//echo curl_errno($ch);
$response = json_decode($data);
echo "url: " . $response->url."<br />";
echo "id: " . $response->id."<br />";
curl_close($ch);
ConversionConversion EmoticonEmoticon