2016-04-11 13 views
1

Ich versuche, die neueste Version von Guzzle (6.2) zu lernen und meine cURL Anfragen in die WHMCS API zu konvertieren.Converting Request von CURL zu PHP Guzzle zum Zugriff auf WHMCS-API

Mit dem Beispielcode aus: 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); 

Ich habe noch nicht gelungen, herauszufinden, wie die gleiche Sache bekommen mit Guzzle zu arbeiten.

Hier ist, was ich versucht habe:

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

Dies wird höchstwahrscheinlich eine sehr offensichtliche Antwort auf diejenigen, die Guzzle vor verwendet haben.

Wohin gehe ich hier falsch?

Antwort

1

Ich glaube, Sie brauchen 'form_params' verwenden urlencoded POST-Daten zu senden:

$username = "Admin"; 
$password = "password123"; 

// Set post values 
$postfields = array(
    'username' => $username, 
    'password' => md5($password), 
    'action' => 'GetClients', 
    'responsetype' => 'json', 
); 

$client = new Client([ 
    'base_uri' => 'https://www.yourdomain.com', 
    'timeout' => 30, 
]); 

$response = $client->request('POST', '/billing/includes/api.php', [ 
    'form_params' => $postfields 
]); 
+0

Das tat es! Vielen Dank! – Muzzstick