このページの翻訳:
- 日本語 (ja)
- English (en)
最近の更新
- 02 Apache2で自己認証ssl [中間証明書の整合性確認]
Tag Cloud
このページへのアクセス
今日: 1 / 昨日: 1
総計: 1415
- Dokuwiki.fl8.jp(247)
- bonnie++(2)
- Tag Cloud(1)
- Gallery(1)
- page list(1)
最近の更新
このページへのアクセス
今日: 1 / 昨日: 1
総計: 1415
Crulより簡単で、シンプルに書ける
guzzlehttp/guzzle 6.3.3
composer require guzzlehttp/guzzle
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();
下記は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;
<?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')
'http_errors' ⇒ false にすると、50X,40Xでも例外を投げられない(処理が止まらない)
$response = $client->request( 'GET', $url, [ 'timeout' => 2, 'verify' => false, 'http_errors' => false, ] );
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()); }