【问题标题】:no result when http authentification via php curl通过 php curl 进行 http 身份验证时没有结果
【发布时间】:2016-02-29 11:43:52
【问题描述】:

我有一个由 http 保护的 Web 服务器。我想通过另一个服务器登录到这个 server1。当我使用这个网址时:http://username:password@server1。有用。

现在我想从 server2 中的另一个网页执行这个 url。我正在使用卷曲。

<?php
$username='username';
$password='password';
$URL='http://server1';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
$result=curl_exec ($ch);
echo $result ;
curl_close ($ch);
?>

这对我不起作用。我只有使用 http 登录的弹出窗口。我的 php 页面有什么问题?

【问题讨论】:

    标签: php html apache curl basic-authentication


    【解决方案1】:

    试试,

    $username                ='username';
    $password                ='password';
    $yourEfforts             = array();
    $yourEfforts['username']=$username; //stored in yourEfforts array.
    $yourEfforts['password']=$password; //stored in yourEfforts array.
    $URL='http://server1';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$URL);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, $yourEfforts);//Your username and password
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
    echo "<pre>";
    var_dump(curl_exec($ch));//debug curl exec output
    var_dump(curl_getinfo($ch));//debug curl info
    var_dump(curl_error($ch));//debug curl error
    $result=curl_exec ($ch);
    echo $result ;
    curl_close ($ch);
    

    希望对你有帮助!

    【讨论】:

    • 我得到了一个带有“未经授权”的页面。
    • 是否返回 401 Unauthorized ?
    • 是的,您的版本返回“401 Unauthorized”。和前一个它得到“301 永久移动 - 文档已移至此处。”
    • // 关闭前检查错误 $result = curl_exec($ch); if ($result === false) { echo curl_error($ch); }
    • 我总是得到“401 Unauthorized”。但是当我通过 URL 使用带有用户名和密码的经典方法尝试使用浏览器时,它可以工作。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2010-10-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2014-05-26
    相关资源
    最近更新 更多