我正在尝试通过URL从ServiceNow启动自动下载CSV文件的操作。
我可以将链接"https://<mycompany>.service-now.com/incident_list.do?CSV&sysparm_query=sys_updated_onBETWEENjavascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)&sysparm_orderby=sys_updated_on"
粘贴到浏览器中,然后自动下载CSV文件。我想使它自动化。我尝试使用javascript,但没有绕过相同原产地政策。我正在尝试使用以下代码编写PHP CURL。
<?php
include 'credentials.php';
function get_web_page( $url)
{
$header = array('content-type: application/x-www-form-urlencoded');
global $user;
global $pass;
$options = array(
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_HTTPHEADER => array('content-type: application/x-www-form-urlencoded'),
CURLOPT_USERPWD => "$user:$pass"
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
$Content_Array = array();
$url = 'https://<mycompany>.service-now.com/incident_list.do?CSV&sysparm_query=sys_updated_onBETWEENjavascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)&sysparm_orderby=sys_updated_on';//
echo '<hr>'.$url.'<hr>';
$page = get_web_page( $url );
echo $content = $page['content'];
echo '<pre>';
print_r($page);
echo '</pre>';
?>
这将返回一系列信息,但不是我需要的信息,并且不会下载任何内容。希望我只是缺少一些简单的东西。
答案 0 :(得分:0)
在您指定输出文件的任何地方都看不到。您可以使用 CURLOPT_FILE 选项和 fopen 自动保存输出。这是我从here借来的精简示例:
$out = fopen($localfilename, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);
fclose($out);