Skip to content

Commit 91a37d8

Browse files
committed
Add support for DELETE and PUT requests
1 parent 2aaa22c commit 91a37d8

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
PHP Cross Domain (AJAX) Proxy
22
==============
33

4-
An application proxy that can be used to transparently transfer any request ( including of course XMLHTTPRequest ) to any third part domain. It is possible to define a list of acceptable third party domains and you are encouraged to do so. Otherwise the proxy is open to any kind of requests.
4+
An application proxy that can be used to transparently transfer all kind of requests ( including of course XMLHTTPRequest ) to any third part domain. It is possible to define a list of acceptable third party domains and you are encouraged to do so. Otherwise the proxy is open to any kind of requests.
55

66
Installation
77
--------------
88

99
The proxy is indentionally limited to a single file. All you have to do is to place `proxy.php` under your application
1010

11-
Whenever you want to make a cross domain request, just make a request to http://www.yourdomain.com/ajax-proxy.php and specify the cross domain URL by using `csurl` parameter. Obviously, you can add more parameters according to your needs; note that the rest of the parameters will be used in the cross domain request. For instance, if you are using jQuery:
11+
Whenever you want to make a cross domain request, just make a request to http://www.yourdomain.com/proxy.php and specify the cross domain URL by using `csurl` parameter. Obviously, you can add more parameters according to your needs; note that the rest of the parameters will be used in the cross domain request. For instance, if you are using jQuery:
1212

1313
``` JAVASCRIPT
1414
$('#target').load(
15-
'http://www.yourdomain.com/ajax-proxy.php', {
15+
'http://www.yourdomain.com/proxy.php', {
1616
csurl: 'http://www.cross-domain.com/',
1717
param1: value1,
1818
param2: value2
1919
}
2020
);
2121
```
2222

23-
It’s worth mentioning that both POST and GET methods work and headers are taken into consideration. That is to say, headers sent from browser to proxy are used in the cross domain request and vice versa.
23+
It’s worth mentioning that all request methods are working GET, PUT, POST, DELETE are working and headers are taken into consideration. That is to say, headers sent from browser to proxy are used in the cross domain request and vice versa.
2424

2525
You can also specify the URL with the `X-Proxy-URL` header, which might be easier to set with your JavaScript library. For example, if you wanted to automatically use the proxy for external URL targets, for GET and POST requests:
2626

proxy.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* AJAX Cross Domain (PHP) Proxy 0.7
4+
* AJAX Cross Domain (PHP) Proxy 0.8
55
* by Iacovos Constantinou (http://www.iacons.net)
66
*
77
* Released under CC-GNU GPL
@@ -48,11 +48,22 @@
4848

4949
// identify request method, url and params
5050
$request_method = $_SERVER['REQUEST_METHOD'];
51-
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
51+
if ( 'GET' == $request_method ) {
52+
$request_params = $_GET;
53+
} elseif ( 'POST' == $request_method ) {
54+
$request_params = $_POST;
55+
} elseif ( 'PUT' == $request_method || 'DELETE' == $request_method ) {
56+
$request_params = file_get_contents( 'php://input' );
57+
} else {
58+
$request_params = null;
59+
}
5260
// Get URL from `csurl` in GET or POST data, before falling back to X-Proxy-URL header.
53-
$request_url = urldecode( isset( $_REQUEST['csurl'] ) ? $_REQUEST['csurl'] : $_SERVER['HTTP_X_PROXY_URL'] );
61+
$request_url = isset( $_REQUEST['csurl'] ) ? urldecode( $_REQUEST['csurl'] ) : urldecode( $_SERVER['HTTP_X_PROXY_URL'] );
5462
$p_request_url = parse_url( $request_url );
55-
unset( $request_params['csurl'] );
63+
64+
// csurl may exist in GET request methods
65+
if ( is_array( $request_params ) && array_key_exists('csurl', $request_params ) )
66+
unset( $request_params['csurl'] );
5667

5768
// ignore requests for proxy :)
5869
if ( preg_match( '!' . $_SERVER['SCRIPT_NAME'] . '!', $request_url ) || empty( $request_url ) || count( $p_request_url ) == 1 ) {
@@ -91,10 +102,13 @@
91102
curl_setopt( $ch, CURLOPT_HTTPHEADER, $request_headers ); // (re-)send headers
92103
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // return response
93104
curl_setopt( $ch, CURLOPT_HEADER, true ); // enabled response headers
94-
// add post data for POST requests
95-
if ( $request_method == 'POST' ) {
105+
// add data for POST, PUT or DELETE requests
106+
if ( 'POST' == $request_method ) {
96107
curl_setopt( $ch, CURLOPT_POST, true );
97108
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $request_params ) );
109+
} elseif ( 'PUT' == $request_method || 'DELETE' == $request_method ) {
110+
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $request_method );
111+
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request_params );
98112
}
99113

100114
// retrieve response (headers and content)
@@ -118,7 +132,7 @@
118132
}
119133

120134
// finally, output the content
121-
print($response_content );
135+
print( $response_content );
122136

123137
function csajax_debug_message( $message )
124138
{

0 commit comments

Comments
 (0)