#php #curl
#php #curl
Вопрос:
У меня есть безопасный сайт, на который я захожу с помощью cURL. Это работает нормально. Затем я вызываю второй URL-адрес с данными post, который создаст файл Excel, который будет возвращен в браузер. Создание файла и отправка его обратно в браузер занимает секунду или две. Я поиграл с настройками cURL, но не могу заставить его сохранить файл на свой сервер. Ниже приведен сценарий, который я пытаюсь:
//curl login url
$login_url = 'https://url.com/control_panel.php';
//init curl
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $login_url);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'amp;password='.$password);
//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
//return results not true/false
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request (the login)
$store = curl_exec($ch);
//the login is now done and you can continue to get the protected content.
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://url.com/buildexcel');
//form variables we want
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$post_data = array(
'from_month' => date('n', $start_date),
'from_day' => date('j', $start_date),
'from_year' => date('Y', $start_date),
'to_month' => date('n', $end_date),
'to_day' => date('j', $end_date),
'to_year' => date('Y', $end_date),
'all_leagues' => 1,
'practices' => 1,
'export' => 'Export Schedule'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//execute the request
$content = curl_exec($ch);
curl_close($ch);
//save the data to disk
$file = './uploads/excel_'.time().'.csv';
file_put_contents($file, $content);
Любая помощь была бы отличной. Я пробовал другие методы, но тоже безуспешно:
$fp = fopen ($local_file, 'w ');
//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://url.com/buildexcel');
//form variables we want
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_ENCODING, "");
$post_data = array(
'from_month' => date('n', $start_date),
'from_day' => date('j', $start_date),
'from_year' => date('Y', $start_date),
'to_month' => date('n', $end_date),
'to_day' => date('j', $end_date),
'to_year' => date('Y', $end_date),
'all_leagues' => 1,
'practices' => 1,
'export' => 'Export Schedule'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//execute the request
$content = curl_exec($ch);
curl_close($ch);
fclose($fp);
После этого код if сохраняет файл, но в браузер отправляется HTML-код со страницы, а не excel.
Ответ №1:
попробуйте это>>> этот код для получения данных из url, но с получением HTTP-запроса, я думаю, вам нужно только (=== важная часть ===)
$ch = curl_init();
$csrf_token = $this->getCSRFToken($ch);// this function to get csrf token from website if you need it
$ch = $this->signIn($ch, $csrf_token);//signin function you must do it and return channel
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);// if file large
curl_setopt($ch, CURLOPT_URL, "https://your-URL/anything");
$return=curl_exec($ch);
//=== the important part === to save file
$destination ="files.xlsx";
if (file_exists( $destination)) {
unlink( $destination);
}
$file=fopen($destination,"w ");
fputs($file,$return);
if(fclose($file))
{
echo "downloaded";
}
curl_close($ch);