如何使用PHP在Blogger中发布帖子?

如何使用PHP在Blogger中发布帖子?,php,curl,blogger,Php,Curl,Blogger,我在如何在Blogger中发布帖子方面遇到了问题,根据他们的观点,查询必须如下所示: POST https://www.googleapis.com/blogger/v3/blogs/{blogId}/posts/ Authorization: {OAuth 2.0 token} Content-Type: application/json { "kind": "blogger#post", "blog": { "id": "{blogId}" }, "title":

我在如何在Blogger中发布帖子方面遇到了问题,根据他们的观点,查询必须如下所示:

POST https://www.googleapis.com/blogger/v3/blogs/{blogId}/posts/
Authorization: {OAuth 2.0 token}
Content-Type: application/json

{
  "kind": "blogger#post",
  "blog": {
    "id": "{blogId}"
  },
  "title": "A new post",
  "content": "With <b>exciting</b> content..."
}
用于插入邮件的代码:

$postTitle = 'post test';
$postContent = 'just another test.';
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blogId.'/posts/';
$headerQuery = array();
$headerQuery[] = 'Authorization: '.$accessToken;
$headerQuery[] = 'Content-Type: application/json';
$headerQuery[] = ' { "kind": "blogger#post", "blog": {"id": "'.$blogId.'"}, "title": "'.$postTitle.'", "content": "'.$postContent.'" }';
//$headerQuery[] = 'Content-length: '.strlen($headerQuery[2]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerQuery);
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);
echo $data;
//echo curl_errno($ch);
$response = json_decode($data);
echo "url: " . $response->url."<br />";
echo "id: " . $response->id."<br />";
curl_close($ch);
$postTitle='post test';
$postContent='只是另一个测试';
$url='1https://www.googleapis.com/blogger/v3/blogs/“.$blogId./posts/”;
$headerQuery=array();
$headerQuery[]=“授权:”。$accessToken;
$headerQuery[]='内容类型:应用程序/json';
$headerQuery[]='{“种类”:“blogger#post”,“blog”:{“id”:“.$blogId.”“},“title”:“.$postTitle.”“,“content”:“.$postContent.”“}”;
//$headerQuery[]=“内容长度:”.strlen($headerQuery[2]);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerQuery);
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\u exec($ch);
回波数据;
//回声旋度(ch);
$response=json_decode($data);
回显“url:”$响应->url。“
”; 回显“id:”$响应->id.“
”; 卷曲关闭($ch);
我在代码中发现了两个错误,第一个错误是针对Google plus进行身份验证,而我应该针对Blogger进行身份验证

<?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;
}

如果(isset($_GET['code'])){
//获取访问令牌
$code=$_GET['code'];
$url='1https://accounts.google.com/o/oauth2/token';
$apikey='XXXXXXXXXXXXXXXXXXXXX';
$blogId='xxxxxxxxxxxxxxxxxxxxx';
$params=数组(
“代码”=>$code,
“客户端id”=>urlencode(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com”),
“客户机密”=>urlencode(“XXXXXXXXXXXXXXXXXXXXXXXXX”),
“重定向_uri”=>urlencode(”http://localhost/phpBlogger/oauth2callback.php"),
“授权类型”=>urlencode(“授权代码”)
);
//HTTP查询生成器
foreach($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\u exec($ch);
卷曲关闭($ch);
//回声旋度(ch);
$response=json_decode($data);
$accessToken=$response->access\u令牌;
}
第二个错误是如何构造JSON POST查询,我应该将内容长度添加到标题中:

        $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);
$postTitle='post test';
$postContent='只是另一个测试';
$url='1https://www.googleapis.com/blogger/v3/blogs/“.$blogId./posts/”;
$body='{“kind”:“blogger#post”,“blog”:{“id”:“.$blogId.”“},“title”:“.$postTitle.”“,“content”:“.$postContent.”“}”;
$headerQuery=array();
$headerQuery[]=“授权:OAuth”。$accessToken;
$headerQuery[]=“内容长度:”.strlen($body);
$headerQuery[]='内容类型:应用程序/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\u exec($ch);
/*var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
回显“


”$data*/ //回声旋度(ch); $response=json_decode($data); 回显“url:”$响应->url。“
”; 回显“id:”$响应->id.“
”; 卷曲关闭($ch);
我在代码中发现了两个错误,第一个错误是针对Google plus进行身份验证,而我应该针对Blogger进行身份验证

<?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;
}

如果(isset($_GET['code'])){
//获取访问令牌
$code=$_GET['code'];
$url='1https://accounts.google.com/o/oauth2/token';
$apikey='XXXXXXXXXXXXXXXXXXXXX';
$blogId='xxxxxxxxxxxxxxxxxxxxx';
$params=数组(
“代码”=>$code,
“客户端id”=>urlencode(“xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com”),
“客户机密”=>urlencode(“XXXXXXXXXXXXXXXXXXXXXXXXX”),
“重定向_uri”=>urlencode(”http://localhost/phpBlogger/oauth2callback.php"),
“授权类型”=>urlencode(“授权代码”)
);
//HTTP查询生成器
foreach($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\u exec($ch);
卷曲关闭($ch);
//回声旋度(ch);
$response=json_decode($data);
$accessToken=$response->access\u令牌;
}
第二个错误是如何构造JSON POST查询,我应该将内容长度添加到标题中:

        $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);
$postTitle='post test';
$postContent='只是另一个测试';
$url='1https://www.googleapis.com/blogger/v3/blogs/“.$blogId./posts/”;
$body='{“kind”:“blogger#post”,“blog”:{“id”:“.$blogId.”“},“title”:“.$postTitle.”“,“content”:“.$postContent.””