Guzzle删除冗余请求方法

问题描述 投票:1回答:1

有没有删除冗余写入请求方法的方法?

所以此时我的代码看起来像这样

$response['slide'] = $this->_client->request('GET', 'artikelc/slidelimit',[
    'query' =>  [
        'auth-apikey' =>    $this->keyauth
    ]
]);
$this->data['slide_grab'] = json_decode($response['slide']->getBody()->getContents());

$response['subpost'] = $this->_client->request('GET', 'artikelc/subpost',[
    'query' =>  [
        'auth-apikey' =>    $this->keyauth
    ]
]);
$this->data['subpost_grab'] = json_decode($response['subpost']->getBody()->getContents());

$response['newsone'] = $this->_client->request('GET', 'artikelc/newsone',[
    'query' =>  [
        'auth-apikey' =>    $this->keyauth
    ]
]);
$this->data['newsone_grab'] = json_decode($response['newsone']->getBody()->getContents());

正如你所看到的,我必须重写相同的代码。我可以让它变得更简单吗?

php json api guzzle
1个回答
1
投票

你可以做的是你可以简化通话:

$options = [
'query' =>  [
    'auth-apikey' =>    $this->keyauth
]];

$this->data['slide_grab'] = $this->_client->get('artikelc/slidelimit', $options)->json();
$this->data['subpost_grab'] = $this->_client->get('artikelc/subpost', $options)->json();
$this->data['newsone_grab'] = $this->_client->get('artikelc/newsone', $options)->json();
© www.soinside.com 2019 - 2024. All rights reserved.