2016-11-21 232 views
4

狂飲我有這樣的笨以下的捲曲要求:轉換捲曲請求Laravel

$order = [ 
    'index' => 'Value', 
    'index2' => 'Value2' 
]; 

$this->curl->create($this->base_url.'order/'); 
$this->curl->http_login($creds['username'], $creds['password']); 
$this->curl->ssl(TRUE, 2, 'certificates/certificate.pem'); 
$this->curl->option(CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json')); 
$this->curl->option(CURLOPT_FAILONERROR, FALSE); 

$this->curl->post(json_encode($order)); 
$data = $this->curl->execute(); 

現在我需要發出的Laravel,在那裏我使用狂飲同樣的要求。我如何將它轉換爲Guzzle請求?

回答

0

非常,非常簡單:

$client = new GuzzleHttp\Client(['base_uri' => $this->base_url]); 

$response = $client->request('POST', 'order/', [ 
    'form_params' => $order, 
    'headers' => [ 
    'Content-Type' => 'application/json', 
    'Accept' => 'application/json' 
    ], 
    'auth' => [$creds['username'], $creds['password']], 
    'http_errors' => false, 
    'verify' => 'certificates/certificate.pem' 
]); 

echo $response->getBody(); 

注意,這有沒有關係Laravel,它只是狂飲。 Laravel不會以任何方式影響Guzzle API。

+0

那麼SSL和證書呢? –

+0

@HappyCoder Guzzle默認支持SSL請求,我假設你的base_url()返回一個以https:// –

+0

@HappyCoder開頭的字符串,但是如果你使用自簽名證書,你可以只添加'verify'=>'證書/certificate.pem'或'verify'=> false以禁用SSL證書檢查 –