从PHP中抓取文件,该文件强制从另一个具有HTTP身份验证的Web服务器PHP中另存为


grab file from php which forced to save as from another web server php with http authentication

  1. 我有一个来自启用了 HTTP 身份验证的另一台服务器的 URL。
  2. 此特定 URL 强制文件下载文件。
  3. 我在这里尝试抓取标题并重新设置标题,以便我可以在 curl 中完成 http 身份验证后强制从我的 php 文件下载,但没有成功。 curl 没有给出任何错误。 $body没有返回任何内容。

    $url=$_GET["url"];//another web server url which forcing file download save as
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERPWD, "username:password");//http auth done here
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, null); 
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);
    list ($headerString, $body) = explode("'r'n'r'n", $response, 2);
    $headers = explode("'r'n", $headerString);
    foreach ($headers as $header) {
        header($header);
    }
    echo $body;
    exit; 
    

上面的代码工作正常。问题只是 &在 $_GET["url"] 中拆分有效 url。刚刚使用了 urlencode 和代码现在工作正常。希望这会帮助某人。