ユーザ用ツール

サイト用ツール


サイドバー



最近の更新



Tag Cloud

タグ数量
9
10
1
3
1
2
17
7
1
1
4
13
4
1
2
309
1
9
2
7
21
5
1
31
3
3
1
6
1
1
46
1
3
3
1
1
1
1
1
1
33
46
7
9
10
8
10
3
21
2
9
2
2
6
9
1
1
6
12
4
3
3
2
1
8
2
4
1
6
30
2
12
5
3
1
1
2
3
2
1
3
4
3
3
4
2
2
1
1
2
1
2
1
2
1
27
2
1
3
2
2
1
1
2
3
17
5
9
1
1
2
16
2
6
1
2
12
1
3
1
1
4
11
1
6
4
2
7
1
3
3
13
1
4
1
1
10
3
1
17
3
1
5
1
2
1
1
2
9
2
2
3
2
1
3
1
3
1
2
2
2
1
2
6
1
4
3
5
1
3
1
3
3
2
1
1
30
1
3
3
1
1
1
5
5
1
36
2
4
2
1
2
2
3
1
1
1
4
1
2
1
3
1
1
1
2
5
2
5
2
1
1
1
5
3
2
4
4
1
2
2
25
1
1
3
4
1
1
2
1
1
2
1
1
1
1
2
1
1
1
1
1
19
7
1
5
1
1
3
1
2
1
1
2
1
1
1
1
1
1
2
1
1
2
1
1
1
1
1
1
1
4
2
1
1
2
2
2
1
1
2
1
1
12
1
1
1
1
1
1
1
1
1
1
1
1
1
11_php:02_framework:01_laravel:35_laravel_guzzle

35 Laravel Curlの代わりにGuzzleを使う

Crulより簡単で、シンプルに書ける

guzzlehttp/guzzle 6.3.3

インストール

composer require guzzlehttp/guzzle

1.シンプル利用

use GuzzleHttp\Client;

$url = 'https://hoge.hogehoge.com';
$ip = '192.168.10.11';
$port = '25';
$client = new Client();
$response = $client->request(
    'POST',
    $url,
    [
    'form_params' => [ 'ip' => $ip, 'port'=> $port],
    'verify' => false
    ]
    );

echo $response->getStatusCode(); // 200
echo $response->getReasonPhrase(); // OK
echo $response->getProtocolVersion(); // 1.1
// レスポンスボディを取得
$responseBody = $response->getBody()->getContents();

2.JSON-RPC 2.0

下記はZabbix apiでアラートを取得する例

use GuzzleHttp\Client;
    $url = "http://hogehoge.net/zabbix/api_jsonrpc.php";
    $auth = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $params = [
      "id"=> 1,
      "auth"=> $auth,
      "jsonrpc" => "2.0",
      "method"=> "alert.get",
      "params"=> [
        "output"=> "extend",
        "sortfield"=> "alertid",
        "sortorder"=> "DESC",
        "limit"=> 3
      ],
    ];
    
    $client = new Client();

    try{
      $response = $client->request(
        'POST',
        $url,
        $params
      );
    } catch (\GuzzleHttp\Exception\ConnectException $e) {
      return json_encode($e->gethandlerContext());
    }
    
    echo $response->getStatusCode(); // 200
    echo $response->getReasonPhrase(); // OK
    echo $response->getProtocolVersion(); // 1.1

    $responseBody = $response->getBody()->getContents();
    return $responseBody;

3.XML_RPC

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;

class XmlRpc extends Controller
{
    protected $url;
    protected $user;
    protected $password;
    public function __construct($url='', $user='', $password='')
    {
        $this->url      = $url;
        $this->user     = $user;
        $this->password = $password;
    }
    public function send($method, $params = [])
    {
        $auth = ['login' => $this->user, 'password' => $this->password];
        $params = [$auth, $params];
        $body     = xmlrpc_encode_request($method, $params);
        //print_r($body);
        $response = (new Client)->post($this->url, [
            'body' => $body,
            'auth' => [$this->user, $this->password]
        ]);
        return $response->getBody()->getContents();
    }
}

呼び出し部分

$XmlRcp =  new App\Http\Controllers\XmlRpc($url, $user, $pass);
$XmlRcp->send('partner10.getKeyInfo', 'PLSK.XXXXXXXX.XXXX')

50X, 40X 対応

'http_errors' ⇒ false にすると、50X,40Xでも例外を投げられない(処理が止まらない)

$response = $client->request(
    'GET',
    $url,
    [
    'timeout' => 2,
    'verify' => false,
    'http_errors' => false,
    ]
    );

Error Handle

timeoutなどのexceptionは下記のように処理する。
「catch (\GuzzleHttp\Exception\ConnectException $e)」でエラーを取得すると、タイムアウト時に終了せずエラーを受け取れる

try{
  // Code
  $response = $client->request(
    $method,
    $url,
    $params
  );
} catch(\GuzzleHttp\Exception\GuzzleException $e) {
  // Handle excpetion
  return json_encode($e->gethandlerContext());
}
11_php/02_framework/01_laravel/35_laravel_guzzle.txt · 最終更新: 2021/02/26 21:21 by matsui

Yesterday:860 Today:852 Total:237465