如何在php中使用curl下载文件?

dan*_*car 5 php curl

如果标题设置为true,我如何使用Curl下载PHP文件?我还可以获取文件的文件名和扩展名吗?

PHP代码示例:

curl_setopt ($ch, CURLOPT_HEADER, 1);
$fp = fopen($strFilePath, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
Run Code Online (Sandbox Code Playgroud)

小智 6

我相信你现在已经找到答案了。但是,我想分享我的脚本,该脚本通过向服务器发送 json 请求,以二进制形式返回文件,然后动态下载,该脚本运行良好。没有必要保存。希望能帮助到你!

注意:您可以避免将 post 数据转换为 json。

<?php

// Username or E-mail
$login = 'username';
// Password
$password = 'password';
// API Request
$url = 'https://example.com/api';
// POST data
$data = array('someTask', 24);
// Convert POST data to json
$data_string = json_encode($data);
// initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Execute cURL and store the response in a variable
$file = curl_exec($ch);

// Get the Header Size
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// Get the Header from response
$header = substr($file, 0, $header_size);
// Get the Body from response
$body = substr($file, $header_size);
// Explode Header rows into an array
$header_items = explode("\n", $header);
// Close cURL handler
curl_close($ch);

// define new variable for the File name
$file_name = null;

// find the filname in the headers.
if(!preg_match('/filename="(.*?)"/', $header, $matches)){
    // If filename not found do something...
    echo "Unable to find filename.<br>Please check the Response Headers or Header parsing!";
    exit();
} else {
    // If filename was found assign the name to the variable above 
    $file_name = $matches[1];
}
// Check header response, if HTTP response is not 200, then display the error.
if(!preg_match('/200/', $header_items[0])){
    echo '<pre>'.print_r($header_items[0], true).'</pre>';
    exit();
} else {
    // Check header response, if HTTP response is 200, then proceed further.

    // Set the header for PHP to tell it, we would like to download a file
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Disposition: attachment; filename='.$file_name);

    // Echo out the file, which then should trigger the download
    echo $file;
    exit;
}

?>
Run Code Online (Sandbox Code Playgroud)


Suj*_*wal 5

使用PHP cURL下载文件或网页并将其保存到文件

<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://news.google.com/news?hl=en&topic=t&output=rss');
/**
* Create a new file
*/
$fp = fopen('rss.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>
Run Code Online (Sandbox Code Playgroud)