我试图使用curl从银行网站获取内容。
http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna
网站是特定的,因为它使用ajax来填充货币兑换表。有一个下载数据到文件的链接,但你必须有相同的会话ID才能做到这一点。
我正在尝试这段代码:
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL,$url);
$cl = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($cl);
@$link = $dom->getElementById('tecajPrn');
echo $suburl = "http://www.zaba.hr".$link->getAttribute('href');
在此之后,我收到了文件的链接,但我无法打开它。
另一个奇怪的情况是,我用curl获得的链接是http://www.zaba.hr/home/ZabaUtilsWeb/utils/tecaj/danasPrn
但是点击图标时的真实链接是http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/prn/62/2014
答案 0 :(得分:0)
你正在搞乱cookie和ajax(可能是!)。这是一个看法。试试这个:
首先向页面发送请求以获取cookie。
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
之后再发出另一个卷曲请求。这次获取json数据:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Referer: http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna"));
curl_setopt($ch, CURLOPT_URL,"http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/danas");
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
您的json可在此变量中使用。使用json_decode()
// now parse json from $cl
print $cl;
任何必要的东西,直接帮助自己!
注意:确保您拥有cookie.txt
文件的写入权限。此外,最好使用c:/test/cookie.txt
或/var/tmp/cookie.txt
等绝对路径。