LoginSignup
4
4

More than 5 years have passed since last update.

Guzzle3でgetしたりputしたりする

Posted at

前回の、Guzzle3でResource Owner Password Credentials Grantでトークンを取得するの続きです。

前回はアクセストークンを取得する所まで終わった。
今回は、トークンを使ってgetしたり、putしたりする事にする。

以下のような感じで書けます。

<?php
require 'vendor/autoload.php';

use Guzzle\Http\Client;

$settings = parse_ini_file('config/settings.ini');

$client = new Client($settings['base_url']);
$client->setDefaultOption('headers',
    array(
        'Content-Type' => 'application/json',
        'Authorization' => "Bearer ${settings['token']}",
    )
);

# get
$response = $client->get(
    $settings['api_path'],
    null # header だが、setDefaultOptionで既に設定している。
)->send()->json();

var_dump($response);

# 金額を+100円する
$response['product']['price'] += 100;

$response = $client->put(
    $settings['api_path'],
    null, # header だが、setDefaultOptionで既に設定している。
    json_encode($response)
)->send()->json();

var_dump($response);

githubに動くやつを上げてます。
https://github.com/mapyo/php-client-sample/tree/0.1.2

所感

なんか、これつかってActive Recordみたくかけるといいなぁ

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4