douan4359
2016-09-14 09:15
浏览 481
已采纳

未定义的常量CURLOPT_GET - 假设为'CURLOPT_GET'

I am building my first ever project from scratch on a lamp stack. I decided to try out the slim api framework. Below you can see i start building a helper function for my api. However I am getting this

error: undefined constant CURLOPT_GET - assumed 'CURLOPT_GET'

and then this

error: curl_setopt() expects parameter 2 to be long, string given

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

The api I am hitting is just json encoded objects, not quite sure why this isn't returning the json...

图片转代码服务由CSDN问答提供 功能建议

我正在灯堆上从头开始构建我的第一个项目。 我决定尝试一下苗条的api框架。 下面你可以看到我开始为我的api构建一个辅助函数。 但是我得到了这个

错误:未定义常量CURLOPT_GET - 假设'CURLOPT_GET'

然后这个

错误:curl_setopt()期望参数2为long,给定字符串

  //主要福音块API调用函数
函数gbCall($ gbRoute){
 // JSON  Headers 
 $ gblCallHeaders [] =“Content-Type:application / json; charset = utf-8”; 
 
 //调用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);  
 
 //获取响应
 $ response = curl_exec($ gblCall); 
 
 //关闭cURL连接
 curl_close($ gblCall); 
 
 //解码响应(将其转换为 一个数组)
 $ response = json_decode($ response,true); 
 
 //返回响应
返回$ response; 
} 
   
 
 

我打的api 只是json编码的对象,不太清楚为什么这不会返回json ...

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doucheng5705 2016-09-14 09:18
    已采纳

    Try using CURLOPT_HTTPGET though I am not sure if it serves your purpose.

    More detail can be found here

    点赞 打赏 评论
  • doubi2014 2016-09-14 09:26

    There is nothing like CURLOPT_GET in the options for cURL that's why that error occured. Take a look at CURL options

    点赞 打赏 评论
  • dongwo5110 2018-01-07 16:18

    It happens when phpxxx-curl was not installed in your machine

    点赞 打赏 评论
  • dttwois6098 2018-01-09 09:35
    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);
    
    点赞 打赏 评论

相关推荐 更多相似问题