-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-proxy.php
53 lines (47 loc) · 1.41 KB
/
wp-proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
*本文件是一个简单的php代理脚本
*文件设置了域名限制,仅代理api.wordpress.org、downloads.wordpress.org,防止被滥用
*将本代理脚本上传至海外服务器后,在WordPress主题functions中添加如下代理,即可实现代理下载
*
* add_filter('pre_http_request', function ($pre, $parsed_args, $url) {
* $host = parse_url($url, PHP_URL_HOST);
* if (!in_array($host, ['api.wordpress.org', 'downloads.wordpress.org'])) {
* return $pre;
* }
*
* $proxy_url = '本文件在海外服务器的Url';
* if (!$proxy_url) {
* return $pre;
* }
*
* return wp_remote_request($proxy_url . '?url=' . urlencode($url), $parsed_args);
* }, 10, 3);
*
*/
if (empty($_GET['url'])) {
exit('未指定URL');
}
$url = $_GET['url'];
$urls = parse_url($url);
if (!in_array($urls['host'], ['api.wordpress.org', 'downloads.wordpress.org', 'wordpress.org'])) {
exit('仅代理WordPress');
}
if (!empty($_POST)) {
$query = http_build_query($_POST);
$options['http'] = array(
'timeout' => 20,
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $query,
);
$context = stream_context_create($options);
} else {
$context = null;
}
$result = file_get_contents($url, false, $context);
$response_headers = $http_response_header;
foreach ($response_headers as $response_header) {
header($response_header);
}
echo $result;