php GuzzleでQiita APIv2で投稿をPOSTする小さなサンプル

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php GuzzleでQiita APIv2で投稿をPOSTする小さなサンプル相关的知识,希望对你有一定的参考价值。

<?php

require 'vendor/autoload.php';
use GuzzleHttp\Client;

class Qiita
{
    private $client;
    private $accessToken;
    private $headers;
    const BASEURL = "https://qiita.com/api/v2/";


    public function __construct(string $accessToken){
        $this->accessToken = $accessToken;
        $this->headers = [
            "Authorization" => "Bearer ".$accessToken,
            "Content-Type" => "application/json"
        ];
        $this->client = new Client([
            "base_uri" => self::BASEURL,
            "headers" => $this->headers
        ]);
    }

    public function postItem(array $param){
        $response = $this->client->request("POST", "items", [
            GuzzleHttp\RequestOptions::JSON => $param
        ]);
        return $response;
    }
}

$qiita = new Qiita("xxxxxxxxxxxxxxxxxxxx");
//↓はパラメータなので適当に書き換える
$param = ["body" => "TEST from APIv2",
    "gist" => false,
    "private" => true,
    "tags" => [["name" => "qiita", "versions"=>["2"]]],
    "title"=>"TEST from API2",
    "tweet" => false];
$result = $qiita->postItem($param);

以上是关于php GuzzleでQiita APIv2で投稿をPOSTする小さなサンプル的主要内容,如果未能解决你的问题,请参考以下文章