XML下载被阻止


xml download - blocked

我正试图从一个波兰网站下载xml文件。最初的几天,它工作,但后来我可以下载这个文件到我的服务器(但我可以打开并下载它在我的计算机上)。在我的服务器上的文件中,应该有xml内容是html内容告诉我,我已经被阻止了。

我试图与网站管理员联系,我想从网站获得xml,他告诉我,我没有被IP地址阻止。问题是,我应该在header中发送什么或者下载这个文件?

我的代码下载xml文件如下,这里是我想下载的xml: http://www.polskatimes.pl/rss/fakty_kraj.xml

$headers[]  = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
$headers[]  = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[]  = "Accept-Language:pl-PL,pl;q=0.8";
$headers[]  = "Accept-Encoding:gzip,deflate,sdch";
$headers[]  = "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$headers[]  = "Keep-Alive:115";
$headers[]  = "Connection:keep-alive";
$headers[]  = "Cache-Control:max-age=0";
$xml_data = file_get_contents($xml,false,stream_context_create(
    array("http" => array('header' => $headers)))); // your file is in the string "$xml" now.
file_put_contents($xml_md5, $xml_data); // now your xml file is saved.

以详细模式请求URL (-v):

* About to connect() to www.polskatimes.pl port 80 (#0)
*   Trying 195.8.99.38... connected
* Connectede to www.polskatimes.pl (195.8.99.38) port 80 (#0)
> GET /rss/fakty_kraj.xml HTTP/1.1
> User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
> Host: www.polskatimes.pl
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 18 Apr 2013 10:40:15 GMT
< Content-Type: text/html; charset=utf8
< Transfer-Encoding: chunked
< Connection: close
< Vary: Accept-Encoding
< Expires: Thu, 18 Apr 2013 10:40:15 GMT
< Cache-Control: max-age=0
(html page with message that I am temporary blocked)
* Closing connection #0

要检查后台发生了什么(以及您实际需要或不需要哪些头),您需要进行一些分析。这没什么神奇的,您可以在命令行上使用一个名为curl的软件来完成。它可用于许多(甚至所有?)计算机平台。

第一步通常是以详细模式(-v)请求URL:

$ curl -v http://www.polskatimes.pl/rss/fakty_kraj.xml
* About to connect() to www.polskatimes.pl port 80 (#0)
*   Trying 195.8.99.38... connected
* Connected to www.polskatimes.pl (195.8.99.38) port 80 (#0)
> GET /rss/fakty_kraj.xml HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: www.polskatimes.pl
> Accept: */*
>
< HTTP/1.1 302 Found
< Date: Wed, 17 Apr 2013 17:39:51 GMT
< Server: Apache
< Set-Cookie: sprawdz_cookie=1; expires=Thu, 17-Apr-2014 17:39:51 GMT
< Location: http://www.polskatimes.pl/rss/fakty_kraj.xml?cookie=1
< Vary: Accept-Encoding
< Content-Length: 0
< Connection: close
< Content-Type: text/html; charset=iso-8859-2
<
* Closing connection #0

显示了请求(以>为前缀)和响应(以<为前缀)报头和响应体(在本例中为空)。正如你所看到的状态是302 Found这意味着作为3xx重定向和位置头告诉在哪里:

Location: http://www.polskatimes.pl/rss/fakty_kraj.xml?cookie=1

如query参数所示,这是一个cookie检查。cookie本身也设置好了:

Set-Cookie: sprawdz_cookie=1; expires=Thu, 17-Apr-2014 17:39:51 GMT

那么在下一步中,我们将重播上一个命令,但这次设置cookie可以通过-b参数来完成:

$ curl -v -b prawdz_cookie=1 http://www.polskatimes.pl/rss/fakty_kraj.xml
* About to connect() to www.polskatimes.pl port 80 (#0)
*   Trying 195.8.99.38... connected
* Connected to www.polskatimes.pl (195.8.99.38) port 80 (#0)
> GET /rss/fakty_kraj.xml HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: www.polskatimes.pl
> Accept: */*
> Cookie: prawdz_cookie=1
>
< HTTP/1.1 200 OK
< Date: Wed, 17 Apr 2013 17:43:52 GMT
< Server: Apache
< Set-Cookie: sesja_gratka=e38fa0eb93705c8de7ae906198494439; expires=Wed, 24-Apr-2013 17:43:52 GMT; path=/; domain=polskatimes.pl
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Vary: Accept-Encoding
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/xml; charset=utf-8
<
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title><![CDATA[Fakty - Kraj]]></title>
    <link>http://www.polskatimes.pl/fakty/kraj/</link>
    <atom:link href="http://www.polskatimes.pl/rss/fakty_kraj.xml" rel="self" type="application/rss+xml"/>
    <description><![CDATA[Materiały z działu Kraj]]></description>
... (cutted)

这马上就成功了。现在真正好的部分:您知道您需要为请求设置cookie curl已经显示了它使用的所有标头:

> GET /rss/fakty_kraj.xml HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: www.polskatimes.pl
> Accept: */*
> Cookie: prawdz_cookie=1

它们中的大多数你不需要关心file_get_contents,第一行以及Host:Accept:行。

User-Agent:报头看起来并没有真正发挥作用,因为curl是被接受的。

所以剩下的就是Cookie:标头了。让我们在PHP中试试:

$ php -r "echo file_get_contents('http://www.polskatimes.pl/rss/fakty_kraj.xml', null, 
stream_context_create(['http'=>['header'=>['Cookie: prawdz_cookie=1']]]));"
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title><![CDATA[Fakty - Kraj]]></title>
    <link>http://www.polskatimes.pl/fakty/kraj/</link>
    <atom:link href="http://www.polskatimes.pl/rss/fakty_kraj.xml" rel="self" 
    type="application/rss+xml"/>
...  (cutted)

这是直接测试,只需要Set-Cookie: prawdz_cookie=1头。