簡體   English   中英

如何使用CURL對PHP執行GET請求

[英]How to do a GET request on PHP using CURL

我有一個簡單的GET請求,我正在嘗試制作並獲得結果。 我已經在Postman中嘗試了它,沒有任何標題或正文,它工作得很好。 我甚至把它放在我的瀏覽器中,它返回了一個很好的結果。 但是,當我在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;

使用此標頭將瀏覽器標頭發送到服務器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM