【问题标题】:Guzzle remove redundant request methodGuzzle 去除多余的请求方法
【发布时间】:2019-02-26 20:52:35
【问题描述】:

有什么方法可以去掉多余的写请求方法吗?

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

$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】:

    您可以做的是简化调用:

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

    【讨论】:

    • 还有其他办法吗?
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多