PHP Curl response ends with a 0

I’m currently working on a solution with an API. I’m trying to make a curl request but for some reason, the response always returns with a 0 at the end. I’m not sure if there’s a particular header I’m missing, but the documentation for the API itself only mentions using these particular headers. The code I’ve got here just needs to get an auth token from the API itself so that I can begin using that for operations with the API. For whatever reason, the API doesn’t include an API Key. But that aside, here is my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class API_Connection {
public $auth_token;
public function __construct() {
$api_settings = get_option('api_settings');
$url = 'https://api.example.com/api-token-auth/';
$data = array(
'username' => $geek_api_settings['username'],
'password' => $geek_api_settings['password'],
);
// get the token
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
));
$result = curl_exec($curl);
$auth_token = $result;
curl_close($curl);
return $auth_token;
}
public function get_auth_token() {
return $this->auth_token;
}
}
</code>
<code>class API_Connection { public $auth_token; public function __construct() { $api_settings = get_option('api_settings'); $url = 'https://api.example.com/api-token-auth/'; $data = array( 'username' => $geek_api_settings['username'], 'password' => $geek_api_settings['password'], ); // get the token $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_HTTPHEADER => array('Content-Type: application/json'), CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), )); $result = curl_exec($curl); $auth_token = $result; curl_close($curl); return $auth_token; } public function get_auth_token() { return $this->auth_token; } } </code>
class API_Connection {
    public $auth_token;

    public function __construct() {
        $api_settings = get_option('api_settings');

        $url = 'https://api.example.com/api-token-auth/';
        $data = array(
            'username' => $geek_api_settings['username'],
            'password' => $geek_api_settings['password'],
        );

        // get the token
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($data),
        ));

        $result = curl_exec($curl);
        
        $auth_token = $result;

        curl_close($curl);

        return $auth_token;
    }

    public function get_auth_token() {
        return $this->auth_token;
    }
}

When I get my response, which is spat out as a json string, it always includes a 0 at the end of the response. Currently the API is throttling me, so an example of the response I’m getting is {"detail":"Request was throttled. Expected available in 86400 seconds.","wait":86400}0.

To include the JS, just for prosperities sake, the JS is the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$('#curlAPIToken').on('click', function(e) {
e.preventDefault();
$.ajax({
url: api_settings.ajax_url,
data: {
action: 'curl_auth_token'
},
method: 'POST',
success: function(response) {
console.log(response);
}
})
});
</code>
<code>$('#curlAPIToken').on('click', function(e) { e.preventDefault(); $.ajax({ url: api_settings.ajax_url, data: { action: 'curl_auth_token' }, method: 'POST', success: function(response) { console.log(response); } }) }); </code>
$('#curlAPIToken').on('click', function(e) {
        e.preventDefault();

        $.ajax({
            url: api_settings.ajax_url,
            data: {
                action: 'curl_auth_token'
            },
            method: 'POST',
            success: function(response) {
                console.log(response);
            }
        })
    });

Just to clarify, the action curl_auth_token is just a very simple function – instantiate the class and then call get_auth_token() after that.

I’ve been searching around and can’t figure out why the 0 comes at the end. I know I’ve seen it before and I’ve solved it before, but I’m more used to React solutions and I can’t seem to solve it on this PHP front, and haven’t found any threads with a solution that’s worked for me. How do I get rid of the 0 at the end of the response? And yes, I’ve tried rtrim(), that hasn’t seemed to work either.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật