我正在尝试学习最新版本的 Guzzle (6.2) 并将我的 cURL 请求转换为 WHMCS API。
使用来自: http ://docs.whmcs.com/API:JSON_Sample_Code的示例代码
// The fully qualified URL to your WHMCS installation root directory
$whmcsUrl = "https://www.yourdomain.com/billing/";
// Admin username and password
$username = "Admin";
$password = "password123";
// Set post values
$postfields = array(
'username' => $username,
'password' => md5($password),
'action' => 'GetClients',
'responsetype' => 'json',
);
// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);
// Attempt to decode response as json
$jsonData = json_decode($response, true);
// Dump array structure for inspection
var_dump($jsonData);
我还没有弄清楚如何让同样的东西与 Guzzle 一起工作。
这是我尝试过的:
use GuzzleHttp\Client;
// The fully qualified URL to your WHMCS installation root directory
$whmcsUrl = "https://www.yourdomain.com/billing/";
// Admin username and password
$username = "Admin";
$password = "password123";
$client = new Client([
'base_uri' => $whmcsUrl,
'timeout' => 30,
'auth' => [$username, md5($password)],
'action' => 'GetClients',
'responsetype' => 'json'
]);
$response = $client->request('POST', 'includes/api.php');
echo $response->getStatusCode();
print_r($response,true);
对于以前使用过 Guzzle 的人来说,这很可能是一个非常明显的答案。
我在哪里错了?