如何使用 CURL 在 PHP 上执行 GET 请求

How to do a GET request on PHP using CURL

我有一个简单的 GET 请求,我正在尝试发出并取回结果。我在没有任何 headers 或 body 的情况下在 Postman 中尝试过,它工作得很好。我什至把它放在我的浏览器中,结果 returns 很好。但是,当我在 PHP 中执行此操作时,我什么也没得到。这就是我的代码的样子。我做错了什么?

        $curl = curl_init();

        curl_setopt($curl,CURLOPT_URL,'http://********/vizportal/api/web/v1/auth/kerberosLogin');
        curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, 0);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, '20');

        $resp = curl_exec($curl);

        echo $resp;

使用此 header 发送 header 类似浏览器到服务器:

$curl = curl_init('http://********/vizportal/api/web/v1/auth/kerberosLogin');
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, '20');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curl, CURLOPT_HEADER, true);
//        curl_setopt($curl, CURLINFO_HEADER_OUT, true); // enable tracking

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding:gzip, deflate, sdch',
    'Accept-Language:en-US,en;q=0.6',
    'Cache-Control:max-age=0',
    'Connection:keep-alive',
    'Host:www.********.tld ', // for example : www.google.com
    'Referer: http://********/vizportal/api/web/v1/auth/kerberosLogin',
    'Upgrade-Insecure-Requests:1',
    'User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
));

$response = curl_exec($curl);
curl_close($curl);