dongman2721 2011-04-02 16:59
浏览 100
已采纳

cURL基本身份验证

I'm trying to access a file from a distant server with

$fp = @fsockopen($ip,$port,$errno,$errstr,1);    
fputs($fp, "GET /randomfiletoget.html HTTP/1.0
User-Agent: Mozilla

");

But the server needs a HTML authentification. How should I pass the login/password to access the file?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongzong7467 2011-04-02 17:14
    关注

    To potential downvoters, I wrote authentification on purpose. Change Host to you domain name. It should return HTML output, with valid username and password.

    function authentification($url, $username, $password){
        $headers = array(
        "Host=example.com",
        "User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11",
        "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language=en-us,en;q=0.5",
        "Accept-Encoding=gzip,deflate",
        "Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7",
        "Date: ".date(DATE_RFC822)
        );
    
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
        return curl_exec($curl);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭