Skip to content

Instantly share code, notes, and snippets.

@kuredev
Created November 4, 2017 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuredev/a6b914eededed82a276700f19cddc436 to your computer and use it in GitHub Desktop.
Save kuredev/a6b914eededed82a276700f19cddc436 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment