2016-09-14 150 views
1

我正在燈堆棧上從頭開始構建我的第一個項目。我決定嘗試一下slim api框架。下面你可以看到我開始爲我的API創建一個輔助函數。但是我得到這個未定義常量CURLOPT_GET - 假定'CURLOPT_GET'

錯誤:未定義的常量CURLOPT_GET - 假設 'CURLOPT_GET'

,然後這個

錯誤:curl_setopt()預計參數2長,串給出

// Main Gospel Blocks API Call Function 
Function gbCall($gbRoute) { 
     // JSON Headers 
    $gblCallHeaders[] = "Content-Type: application/json;charset=utf-8"; 

    // Call the API 
    $gblCall = curl_init(); 
    curl_setopt($gblCall, CURLOPT_URL, $GLOBALS['gbApiUrl'] . $gbRoute); 
    curl_setopt($gblCall, CURLOPT_GET, TRUE); 
    curl_setopt($gblCall, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($gblCall, CURLOPT_HTTPHEADER, $gblCallHeaders); 

    // Get the response 
    $response = curl_exec($gblCall); 

    // Close cURL connection 
    curl_close($gblCall); 

    // Decode the response (Transform it to an Array) 
    $response = json_decode($response, true); 

    // Return response 
    return $response; 
} 

我打的api只是json編碼的對象,不太清楚爲什麼這不返回json ...

回答

0

有一個在選項捲曲沒有像CURLOPT_GET。這就是這個錯誤發生的原因。看到

options

0

phpxxx-curl沒有安裝在你的機器

0
For the GET Request in the Curl 



    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "URL"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 

    $headers = array(); 
    $headers[] = "Key: Value"; 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    $result = curl_exec($ch); 
    if (curl_errno($ch)) { 
     echo 'Error:' . curl_error($ch); 
    } 

    curl_close ($ch);