2016-05-08 51 views
2

我無法使用CURL進行API調用。下面是如何使用cURLCURL給出500內部服務器錯誤

$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); 
+0

請打印curl_error($ ch);並告訴它返回什麼 –

+0

它沒有顯示任何信息,但當我評論第一行$ ch = curl_init(「http://sms.geekapplications.com/api/balance.php?authkey=2011AQTvWQjrcB56d9b03d&type=4」);一切工作正常,如果我打印任何消息。 – Himanshu

+2

如果這是您的真實身份驗證密鑰,則可能需要在將它發佈到Internet上後進行更改。 –

回答

0

首先,你可能想使用的功能爲這個..和您的捲曲它無法正確生成API調用的代碼。請參閱我的例子

//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...'; 
    } 
} 

使用它你的控制器內嘗試print_r($this->getBalance());應該輸出與平衡的數組。

+2

不要在json解碼中使用'@'我知道在解碼過程中遇到json錯誤的可能性接近於零,但不能抑制錯誤是很好的 – Gntem

+0

@GeoPhoenix是的,你是對的。只是有時如果php沒有配置json_decode會顯示php itslef的錯誤信息..所以@只是忽略它 –

+0

可以請你建議我如何顯示這個值在查看按鈕點擊當上述功能在控制器中調用 – Himanshu