shinke1987.net
雑多な備忘録等のはず。
他のカテゴリ・タブ
目次
PR

HTTPクライアントGuzzleをセットアップと簡易動作確認

2023-05-24 2023-05-24

セットアップ

最新のGuzzleを設置するために次のコマンドをアプリルートで実行すると、

> composer require guzzlehttp/guzzle *

次のように表示される。

Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been created
Running composer update guzzlehttp/guzzle
Loading composer repositories with package information
Updating dependencies
Lock file operations: 8 installs, 0 updates, 0 removals
  - Locking guzzlehttp/guzzle (7.7.0)
  - Locking guzzlehttp/promises (2.0.0)
  - Locking guzzlehttp/psr7 (2.5.0)
  - Locking psr/http-client (1.0.2)
  - Locking psr/http-factory (1.0.2)
  - Locking psr/http-message (2.0)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking symfony/deprecation-contracts (v3.2.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 8 installs, 0 updates, 0 removals
    Failed to download symfony/deprecation-contracts from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing symfony/deprecation-contracts (v3.2.1) into cache
    Failed to download psr/http-message from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing psr/http-message (2.0) into cache
    Failed to download psr/http-client from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing psr/http-client (1.0.2) into cache
    Failed to download ralouphie/getallheaders from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing ralouphie/getallheaders (3.0.3) into cache
    Failed to download psr/http-factory from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing psr/http-factory (1.0.2) into cache
    Failed to download guzzlehttp/psr7 from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing guzzlehttp/psr7 (2.5.0) into cache
    Failed to download guzzlehttp/promises from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing guzzlehttp/promises (2.0.0) into cache
    Failed to download guzzlehttp/guzzle from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini
    Now trying to download from source
  - Syncing guzzlehttp/guzzle (7.7.0) into cache
  - Installing symfony/deprecation-contracts (v3.2.1): Cloning e2d1534420 from cache
  - Installing psr/http-message (2.0): Cloning 402d35bcb9 from cache
  - Installing psr/http-client (1.0.2): Cloning 0955afe482 from cache
  - Installing ralouphie/getallheaders (3.0.3): Cloning 120b605dfe from cache
  - Installing psr/http-factory (1.0.2): Cloning e616d01114 from cache
  - Installing guzzlehttp/psr7 (2.5.0): Cloning b635f279ed from cache
  - Installing guzzlehttp/promises (2.0.0): Cloning 3a494dc7dc from cache
  - Installing guzzlehttp/guzzle (7.7.0): Cloning fb7566cacc from cache
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found

composerを実行したフォルダ(アプリルート)に作成されるcomposer.jsonを開き、
次のように記載されていることを確認する。

{
    "require": {
        "guzzlehttp/guzzle": "*"
    }
}

動作確認(参考書:独習PHPのサンプルサイトを利用)

下記コードを設置し、

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

// クライアントを生成
$cli = new GuzzleHttp\Client([
    'base_uri' => 'https://wings.msn.to/'
]);

// リクエストを送信
$res = $cli -> request('get', '/tmp/sample.txt');

// 取得したコンテンツを出力
print ("<b>getBody()メソッドからの表示<br></b>");
print ($res -> getBody() . '<br><br>');

// jsonファイルの内容をrequestメソッドを利用して出力
$res = $cli -> request('get', '/tmp/books.json');
$obj = json_decode($res -> getBody());
print ("<b>request()メソッドからの表示<br></b>");
print_r ($obj);
print ('<br><br>');

// jsonファイルの内容をgetメソッドを利用して出力
$res = $cli -> get('/tmp/books.json');
$obj = json_decode($res -> getBody());
print ("<b>get()メソッドからの表示<br></b>");
print_r($obj);
print ('<br><br>');

// jsonファイルの内容を格納した変数のvar_dump
print ("<b>var_dump()メソッドからの表示<br></b>");
var_dump($obj);
print('<br><br>');

// jsonファイル内の特定の要素を取得
print ('<b>$obj -> books[0] -> title<br></b>');
print ($obj -> books[0] -> {'title'});
print ('<br><br>');

// POSTを行い、結果を表示する。
$res = $cli -> post('/tmp/post.php', [
    'form_params' => [
        'name' => 'テストネーム'
    ]
]);
print ('<b>POSTでnameにテストネームを送信し、結果を表示する<br></b>');
print ($res -> getBody());

Webブラウザからアクセスすると、次の結果が表示された。

同一カテゴリの記事