我一直试图以两种不同的方式从API下载一个文件,但没有成功:https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today。
*注意,该文件没有以.csv结尾,它只是弹出该文件的下载文件。
下载的文件是一个.CSV文件。
我试着用卷发:
// Date looks like this: 2016-01-31
$today = date("Y-m-d");
$output_filename = "test.csv";
$host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "https://www.example.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
print_r($result); // prints the contents of the collected file before writing..
// the following lines write the contents to a file in the same directory (provided permissions etc)
$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);print_r($result))后,屏幕上没有打印任何内容。我尝试使用file_put_contents函数:
$today = date("Y-m-d");
echo $today;
file_put_contents("", fopen("https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today", 'r'));
// I TRIED THIS ONE TOO:
// file_put_contents("temp.csv", "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today");*我在第一行(https://example.com/export/banana/by_date/v4?api_token=666&from=2016-01-31&to=2016-01-31)得到一个包含URL的CSV文件。
有人能帮我告诉我如果我做得对吗?(因为这不是文件的直接链接,所以我的工作方式可能不对)。什么才是正确的方法。
发布于 2016-01-31 16:09:27
目标url是https,因此您可能需要添加特定的ssl选项。
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, realpath( '/path/to/cacert.pem' ) );导致curl请求失败的另一个常见原因是缺少useragent字符串。
curl_setopt( $ch, CURLOPT_USERAGENT, 'my useragent string' );在使用file_get_contents时,可以通过为$context设置选项来设置类似的选项
根据您最后的评论,添加:
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );发布于 2016-01-31 16:08:35
这是众所周知的问题(对我来说),使用cURL从php访问https资源--它无法在默认配置中验证证书。因此,要让这个脚本工作起来很容易,您应该为curl_config添加两行代码:
$today = date("Y-m-d");
$output_filename = "test.csv";
$host = "https://example.com/export/banana/by_date/v4?api_token=666&from=$today&to=$today";
$ch = curl_init();
$curl_config = [
CURLOPT_URL => $host,
CURLOPT_VERBOSE => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_AUTOREFERER => false,
CURLOPT_REFERER => "https://www.example.com",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HEADER => 0,
CURLOPT_SSL_VERIFYHOST => 0, //do not verify that host matches one in certifica
CURLOPT_SSL_VERIFYPEER => 0, //do not verify certificate's meta
];
curl_setopt_array($ch, $curl_config); //apply config
$result = curl_exec($ch);
if (empty($result)){
echo curl_error($ch); //show possible error if answer if empty and exit script
exit;
}
curl_close($ch);
print_r($result); // prints the contents of the collected file before writing..
// the following lines write the contents to a file in the same directory (provided permissions etc)
file_put_contents($output_filename, $result);https://stackoverflow.com/questions/35115587
复制相似问题