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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)