将curl请求转换为guzzle

时间:2016-07-07 16:00:33

标签: php http curl guzzle

我正在尝试通过guzzle lib发出curl请求。 我的卷曲请求没有问题:

$memberId = md5(strtolower($data['email']));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;

$json = json_encode([
    'email_address' => $data['email'],
    'status'        => $data['status'],
    'merge_fields'  => [
        'FNAME'     => $data['firstname'],
        'LNAME'     => $data['lastname']
    ]
]);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return $result;

如何将此请求转换为Guzzle方法?

1 个答案:

答案 0 :(得分:1)