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

StackOverflow https://stackoverflow.com/questions/10812958

  •  11-06-2021
  •  | 
  •  

Pergunta

  1. i have a url from another server which having http auth enabled.
  2. this specific url forces file to download a file.
  3. I am trying here to grab the headers and set the headers back so that i can force the download from my php file after http auth done in curl, but no success. curl did not gave any error. nothing returned in $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; 
    
Foi útil?

Solução

code above is working fine. Issue is just & in the $_GET["url"] which splitting valid url. just used urlencode and code working fine now. Hope this will help someone.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top