Himanshu Himanshu - 3 months ago 41
PHP Question

CURL gives 500 Internal server error

I am unable to make the API call using CURL. Below is the code for making the API call using CURL

$ch=curl_init("http://sms.geekapplications.com/api/balance.php?authkey=2011AQTvWQjrcB56d9b03d&type=4");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Authorization: Bearer"));

// execute the api call
$result = curl_exec($ch);
echo ($result);

Answer

First you might wanna be using a function for this.. and your CURL it not build correctly. Please see my example

//gets geekapplications SMS balance
function getBalance() {
    $url = 'http://sms.geekapplications.com/api/balance.php?' . http_build_query([
            'authkey' => '2011AQTvWQjrcB56d9b03d',
            'type' => '4'
        ]);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($http == 200) {
        $json = @json_decode($response, TRUE);
        return $json;
    } else {
        echo 'There was a problem fetching your balance...';
    }
}

Use it within your controller try print_r($this->getBalance()); should output an array with your balance.