我每天都有重复的任务。登录Web门户,单击弹出打开新窗口的链接,然后单击按钮下载Excel电子表格。这是一项耗时的任务,我想自动化。
我一直在用PHP和cUrl做一些研究,虽然它看起来应该是可能的,但我还没有找到任何好的例子。有没有人做过这样的事情,或者你知道哪种工具更适合它吗?
答案 0 :(得分:0)
您熟悉HTTP请求的基础知识吗?比如,你知道POST和GET请求之间的区别吗?如果您所做的只不过是GET请求,那么它实际上非常简单,您根本不需要使用cURL。但如果"点击按钮"表示提交POST表单,那么您将需要cURL。
检查此方法的一种方法是使用Live HTTP Headers等工具,并观察点击链接/按钮时发生的请求。您需要确定哪些变量需要与每个请求一起传递以及您需要使用哪些URL。
但假设至少有一个POST请求,这里有一个基本脚本,它将发布数据并返回任何HTML返回。
<?php
if ( $ch = curl_init() ) {
$data = 'field1=' . urlencode('somevalue');
$data .= '&field2[]=' . urlencode('someothervalue');
$url = 'http://www.website.com/path/to/post.asp';
$userAgent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$html = curl_exec($ch);
curl_close($ch);
} else {
$html = false;
}
// write code here to look through $html for
// the link to download your excel file
?>
答案 1 :(得分:0)
尝试这个>>>
$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
$destination ="files.xlsx";
if (file_exists( $destination)) {
unlink( $destination);
}
$file=fopen($destination,"w+");
fputs($file,$return);
if(fclose($file))
{
echo "downloaded";
}
curl_close($ch);