简体   繁体   English

从银行网站获取货币汇率

[英]Get currency exchange rate from bank site

I'm trying to get content from bank site using curl. 我正在尝试使用curl从银行网站获取内容。

http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna

Site is specific becouse it using ajax to fill currency exchange table. 网站是特定的,因为它使用ajax填写货币兑换表。 There is a link for download data in to file but you have to have same session id to able to do that. 有一个将数据下载到文件中的链接,但是您必须具有相同的会话ID才能执行此操作。

Im trying this code: 我正在尝试以下代码:

$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');

After this I got link to file but I can't open it. 在此之后,我获得了指向文件的链接,但无法打开它。

Another strange situation is that link I got with curl is http://www.zaba.hr/home/ZabaUtilsWeb/utils/tecaj/danasPrn but real link when I click on icon is http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/prn/62/2014 另一个奇怪的情况是,我通过curl获得的链接是http://www.zaba.hr/home/ZabaUtilsWeb/utils/tecaj/danasPrn但是当我单击图标时,真正的链接是http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/prn/62/2014

You are messing with cookie and ajax(may be!). 您搞砸了cookie和ajax(可能是!)。 Here is the lookaround. 这是环顾四周。 Try this: 尝试这个:

First send a request to the page to obtain the cookie. 首先向页面发送请求以获取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);

After that make another curl request. 之后,再次发出卷曲请求。 This time to obtain the json data: 这次获取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);

Your json is available at this variable. 您的json在此变量处可用。 Parse it using json_decode() 使用json_decode()解析

// now parse json from $cl
print $cl;

Anything required, help yourself straightway! 所需的任何东西,请立即自助!

Note: Make sure you have write permission for the cookie.txt file. 注意:确保您具有cookie.txt文件的写许可权。 Also, its better to use absolute path like c:/test/cookie.txt or /var/tmp/cookie.txt . 另外,最好使用绝对路径,例如c:/test/cookie.txt/var/tmp/cookie.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM