아무거나

라이브러guzzle http client 동기/비동기 요청 본문

PHP/PHP

라이브러guzzle http client 동기/비동기 요청

전봉근 2019. 4. 8. 16:05
반응형

[php guzzle http client 동기/비동기 요청]

http://docs.guzzlephp.org/en/stable/index.html // guzzle은 curl 통신을 위한 라이브러리 이다.

 

public function __construct($vendorName, $wmpVendorId, $dataProviderType, $dataProviderProcessType, $startTime)
{
	$this->url = getenv('INTERNAL_API_HOST').'/v1/systems/operation/logs';
	$this->serverIp = getenv('SERVER_ADDR');
	$this->serverDomain = getenv('HTTP_HOST');
	$this->vendorName = $vendorName;
	$this->wmpVendorId = $wmpVendorId;
	$this->dataProviderType = $dataProviderType;
	$this->dataProviderProcessType = $dataProviderProcessType;
	$this->startTime = $startTime;
}



// 동기 ex)
    public function setLog($logLevel=3, $productCnt=0, $operatingTime=0, $msg='')
    {
        $body = [
            'logLevel' => $logLevel,
            'projectType' => self::PROJECT_TYPE,
            'vendorName' => $this->vendorName,
            'wmpVendorId' => $this->wmpVendorId,
            'dataProviderType' => $this->dataProviderType,
            'dataProviderProcessType' => $this->dataProviderProcessType,
            'productCnt' => $productCnt,
            'operatingTime' => $operatingTime,
            'msg' => $msg,
            'serverIp' => $this->serverIp,
            'domain' => $this->serverDomain,
            'startTime' => $this->startTime
        ];

        $client = new Client();
        $client->request('POST', $this->url, [
            'headers' => [
                'content-type'=> 'application/json; charset=utf8'
            ],
            'connect_timeout' => self::CONNECT_TIMEOUT,
            'timeout' => self::TIMEOUT,
            'body' => json_encode($body)
        ]);
    }



// 비동기 ex)
    public function setLogAsync($logLevel=3, $productCnt=0, $operatingTime=0, $msg='')
    {
        $body = [
            'logLevel' => $logLevel,
            'projectType' => self::PROJECT_TYPE,
            'vendorName' => $this->vendorName,
            'wmpVendorId' => $this->wmpVendorId,
            'dataProviderType' => $this->dataProviderType,
            'dataProviderProcessType' => $this->dataProviderProcessType,
            'productCnt' => $productCnt,
            'operatingTime' => $operatingTime,
            'msg' => $msg,
            'serverIp' => $this->serverIp,
            'domain' => $this->serverDomain,
            'startTime' => $this->startTime
        ];

        $client = new Client();
        $stream = stream_for(json_encode($body));
        $client->request('POST', $this->url, [
            'headers' => [
                'content-type'=> 'application/json; charset=utf8'
            ],
            'connect_timeout' => self::CONNECT_TIMEOUT,
            'timeout' => self::TIMEOUT,
            'body' => $stream
        ]);
    }​ 
반응형
Comments