我有一个cron作业,它每天使用命令运行,比如wget -O- "https://www.replaced-with-example-domain.com/cron/export-all-files" >> /dev/null,它创建一个excel文件并上传到服务器中。
上面的命令导致的ERROR 403: Forbidden
url /cron/export-all-files指向一个空白页(没有身份验证),其中只有jquery才能访问另一个url (也没有身份验证)。
视图页(export.php)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
const baseUrl = "https://www.replaced-with-example-domain.com/";
$(document).ready(function(){
$.ajax({
type: 'post',
url: `${baseUrl}sales/invoice-list/export`,
data: {
'export': true,
'cron': true
},
success: function(res){
console.log(res)
}
})
})
</script>如果我在浏览器中手动运行url (在命令中),它可以在没有任何问题的情况下执行,但是当使用cron作业运行时,它将抛出禁止的错误。
如果我在cron命令中添加--user-agent="Mozilla" (引用这),它将提供以下输出
--2022-06-05 12:16:01-- https://www.replaced-with-example-domain.com/cron/export-all-files
Resolving www.replaced-with-example-domain.com (www.replaced-with-example-domain.com)... xxx.x.xxx.17
Connecting to www.replaced-with-example-domain.com (www.replaced-with-example-domain.com)|xxx.x.xxx.17|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 488 [text/html]
Saving to: ‘STDOUT’
0K 100% 70.1M=0s
2022-06-05 12:16:01 (70.1 MB/s) - written to stdout [488/488]但没有创建和上传excel文件,所以我想知道它是否真的工作。正确的方法是什么?谢谢。
发布于 2022-06-06 08:10:38
url
/cron/export-all-files指向一个空白页(没有身份验证),其中只有jquery才能访问另一个url (也没有身份验证)。
Wget不支持JavaScript执行,因此不支持jQuery。如果需要在页面上执行JavaScript的效果,则需要使用不同的工具,例如PhantomJS。你需要先安装它。为了满足您的需要,下面应该足够了。
var page = require('webpage').create();
page.open('http://www.example.com', function(status) {
console.log("Status: " + status);
phantom.exit();
});它只需打开http://www.example.com输出状态并退出。将地址更改为您的需要,并将上面的地址保存为loadpage.js,然后使用它作为phantomjs loadpage.js。
https://stackoverflow.com/questions/72505281
复制相似问题