Without class, just plain code my method is working and getting "SUCCESS" response_status, but when i put it in a class i get nothing.
All code:
class THBS_API
{
private const call_url = "https://xxxxxxxxxxxx.xxx/endsoftware.php";
private $base_url;
private $fields;
private $fields_string;
public static function getUsers()
{
$base_url = $_SERVER['SERVER_NAME'];
$fields_string = '';
$fields = array(
'API_USER' => urlencode('xxxxxx@xxxxxx.xxx'),
'API_PASS' => urlencode('xxxxxxxxx'),
'CMD' => urlencode('getUserCount'),
'FROM' => urlencode($base_url));
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $call_url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($result, true);
return $response['response_status'];
}
}
echo THBS_API::getUsers();
Is very weird, but i think is because declaring variables is different in classes, is the problem i use privar $base_url
? and how to solve this?
P.S: that is my first class ever, i worked fine with normal functions, but i still want to learn classes functions
Now i get
Notice: Undefined variable: fields_string in /srv/disk1/3203065/www/xxxxxxxxxxxxxxxxxx.com/API/clientapi.php on line 24
Line 44:
return $response['response_status'];
Code without class (working):
$base_url = $_SERVER['SERVER_NAME'];
$url = "https://xxxxxxxxxxx.com/API/endsoftware.php";
$fields = array(
'API_USER' => urlencode('xxxxxx@xxxxx.xxxx'),
'API_PASS' => urlencode('xxxxxxxxxx'),
'CMD' => urlencode('getUserCount'));
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$response = json_decode($result, true);
echo $response['response_status'];
(returns "SUCCESS")