Php 自动在Blogspot上发布帖子

Php 自动在Blogspot上发布帖子,php,google-api,google-oauth,blogger,blogspot,Php,Google Api,Google Oauth,Blogger,Blogspot,您好,我正在尝试开发一个功能,将使我的博客内的代码没有任何形式的授权或行动从网站浏览 我需要发送两个参数,帖子标题和帖子内容 我们可以分配$title和$content 我已经工作和研究了大约两周了。几乎没有关于如何做到这一点的文档,现有的文档已经过时,很难使用 任何帮助都将不胜感激 我已经能够使用Blogger V3 API公钥正确配置我的API密钥。我得到了json数据的有效返回,尽管我只需要知道如何发表文章 谢谢大家! <?php function file_get_content

您好,我正在尝试开发一个功能,将使我的博客内的代码没有任何形式的授权或行动从网站浏览

我需要发送两个参数,帖子标题和帖子内容

我们可以分配$title和$content

我已经工作和研究了大约两周了。几乎没有关于如何做到这一点的文档,现有的文档已经过时,很难使用

任何帮助都将不胜感激

我已经能够使用Blogger V3 API公钥正确配置我的API密钥。我得到了json数据的有效返回,尽管我只需要知道如何发表文章

谢谢大家!

<?php

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


$blogID = '2139447653782748476'; 
$apikey = '***';

$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";

//$requestURL = "http://icanhazip.com";

//$json = file_get_contents("http://icanhazip.com");

$json = file_get_contents_curl($requestURL);

$json = json_decode($json);

//echo $requestURL;

//var_dump($json);

var_dump($json);

//echo $json;

?>

我试过的第二个代码:

<?php
/*
function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}


if ($_GET['key'] != 'buswell') {
die();
}
/*
$blogID = '2139447653782748476'; 
$apikey = 'AIzaSyDbouGxlQ9P9JMuH4SJlq43xMhyVGWe14g';

$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";

//$requestURL = "http://icanhazip.com";

//$json = file_get_contents("http://icanhazip.com");

$json = file_get_contents_curl($requestURL);

$json = json_decode($json);

//echo $requestURL;

//var_dump($json);

var_dump($json);

//echo $json;

*/

session_start();
require_once dirname(__FILE__).'/google-api-php-client-master/src/Google/Client.php';
require_once dirname(__FILE__).'/google-api-php-client-master/src/Google/Service/Client.php';

$scriptUri = "http://connectionincognito.com/dev.php";

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

$blogger = new Google_BloggerService($client);

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
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
}

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;
}
//you can get the data about the blog by getByUrl
$data = $blogger->blogs->getByUrl(array('url'=>'http://proxies-unlimited.blogspot.com/'));

//creates a post object
$mypost = new Google_Post();
$mypost->setTitle('this is a test 1 title');
$mypost->setContent('this is a test 1 content');

$data = $blogger->posts->insert('2139447653782748476', $mypost); //post id needs here - put your blogger blog id
 var_dump($data);
?>


您可能希望从使用Php客户端库开始—看起来您当前没有使用它。您好,谢谢您的帮助,我将立即回复,因为我非常感谢先生。请检查我在下面发布的代码,该代码正在使用库。请将下面的代码添加到您的问题中,因为这可能会被删除,因为它不是答案。非常感谢您的建议,我已更新了我的帖子!