目次

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());
}