2
$fileUrl = 'http://127.0.0.1/ReportServer/Pages/ReportViewer.aspx?/newreport&rs:Format=PDF&Name='.$name;

По такому URL хочу загрузить на сервер отчет. Если его дать внутри a href то он грузит этот отчет. А внутри функции нет.Точнее пустой файл грузится. Статус код 401. Как я понял он требует аутентификацию. Как в URL логин и пароль передать, либо как его удалить? Код PHP для загрузки:

    $name = $_GET['name'];
    $fileUrl = 'http://127.0.0.1/ReportServer/Pages/ReportViewer.aspx?/newreport&rs:Format=PDF&Name='.$name;
    $random = time();
    $saveTo = 'C:\xampp\htdocs\test2\ '.$random.'.pdf';

 //Open file handler.
    $fp = fopen($saveTo, 'w+');

 //If $fp is FALSE, something went wrong.
    if($fp === false){
        throw new Exception('Could not open: ' . $saveTo);
    }

//Create a cURL handle.
    $ch = curl_init($fileUrl);

//Pass our file handle to cURL.
    curl_setopt($ch, CURLOPT_FILE, $fp);

//Timeout if the file doesn't download after 20 seconds.
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

 //Execute the request.
    curl_exec($ch);

 //If there was an error, throw an Exception
    if(curl_errno($ch)){
        throw new Exception(curl_error($ch));
    }

//Get the HTTP status code.
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if($statusCode == 200){
        echo 'Downloaded!';
    } else{
        echo "Status Code: " . $statusCode;
    }
}

1 ответ 1

2

После

$ch = curl_init($fileUrl);

Добавьте

curl_setopt($ch, CURLOPT_USERPWD, $login . ':' . $password); 

Где разумеется: $login - логин, $password - пароль

1
  • Не помогло. Так же пустой грузит.
    – Улан
    5 окт 2018 в 4:43

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.