使用 PHP 发出获取请求并将数据复制到服务器
Make get request and copy data to server with PHP
我想使用 cronjob 每 60 分钟发出一次 GET 请求,以便在我的网络服务器上缓存 RSS 提要。我的托管商提供了一个 Web 界面来创建 cronjob,所以这应该很容易做到。由于我几乎没有使用 php 的经验,因此我正在为这部分而苦苦挣扎。我的代码目前看起来像这样:
<?php
$response = http_get("http://www.target-url.com/feed.rss");
$myfile = fopen("rp.xml", "w") or die("Unable to open file!");
fwrite($myfile, $response);
?>
这会将所有内容写入rp.xml,但它也会写入headers,所以我得到无效的xml。 rp.xml 的内容如下所示:
HTTP/1.1 200 OK
Date: Wed, 28 Jan 2015 20:27:03 GMT
Content-Type: application/rss+xml;charset=utf-8
Connection: keep-alive
Set-Cookie: creid=1491575037291426898; expires=Thu, 31-Dec-37 23:55:55 GMT; domain=.target-url.com; path=/; httpOnly
X-Served-By-CC: s19lpay01
X-Cache-Control-Set-By: X-Set-Cache-TTL (300)
Cache-Control: public, max-age=300
Access-Control-Allow-Origin: *
Last-Modified: Wed, 28 Jan 2015 20:23:16 GMT
Content-Length: 10672
Edge-Control: max-age=300
X-Cache: HIT (13)
X-Served-By: RFCTC01
Accept-Ranges: bytes
X-Age: 227
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
{...rest of the xml}
您可以改用 file_get_contents():
string file_get_contents ( string $filename [, bool $use_include_path = false [,
resource $context [, int $offset = -1 [, int $maxlen ]]]] )
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Note: if you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
我想使用 cronjob 每 60 分钟发出一次 GET 请求,以便在我的网络服务器上缓存 RSS 提要。我的托管商提供了一个 Web 界面来创建 cronjob,所以这应该很容易做到。由于我几乎没有使用 php 的经验,因此我正在为这部分而苦苦挣扎。我的代码目前看起来像这样:
<?php
$response = http_get("http://www.target-url.com/feed.rss");
$myfile = fopen("rp.xml", "w") or die("Unable to open file!");
fwrite($myfile, $response);
?>
这会将所有内容写入rp.xml,但它也会写入headers,所以我得到无效的xml。 rp.xml 的内容如下所示:
HTTP/1.1 200 OK
Date: Wed, 28 Jan 2015 20:27:03 GMT
Content-Type: application/rss+xml;charset=utf-8
Connection: keep-alive
Set-Cookie: creid=1491575037291426898; expires=Thu, 31-Dec-37 23:55:55 GMT; domain=.target-url.com; path=/; httpOnly
X-Served-By-CC: s19lpay01
X-Cache-Control-Set-By: X-Set-Cache-TTL (300)
Cache-Control: public, max-age=300
Access-Control-Allow-Origin: *
Last-Modified: Wed, 28 Jan 2015 20:23:16 GMT
Content-Length: 10672
Edge-Control: max-age=300
X-Cache: HIT (13)
X-Served-By: RFCTC01
Accept-Ranges: bytes
X-Age: 227
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
{...rest of the xml}
您可以改用 file_get_contents():
string file_get_contents ( string $filename [, bool $use_include_path = false [,
resource $context [, int $offset = -1 [, int $maxlen ]]]] )
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Note: if you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().