Created
November 4, 2017 02:09
-
-
Save kuredev/a6b914eededed82a276700f19cddc436 to your computer and use it in GitHub Desktop.
GuzzleでQiita APIv2で投稿をPOSTする小さなサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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