tags:

views:

73

answers:

3

i have a hosted script somewhere that only accept POST request. example, some.hosted/script.php

how can i setup another simple php that can accept GET request and then POST it to the hosted script. so that i can put up a link like this: other.site/post2hostedscript.php?postthis=data and then it POST postthis=data to the hosted script.

tnx

edit: post2hostedscript.php do not give any result. the result will go directly to some.hosted/script.php just as if the user POST directly at the hosted script.

+1  A: 

Take a look at the "curl" functions, they provide everything you need.

dbemerlin
+3  A: 

Your post2hostedscript.php will have to :

  • Fetch all parameters received as GET
  • Construct a POST query
  • Send it
  • And, probably, return the result of that POST request.


This can probably be done using curl, for instance ; something like this should get you started :

$queryString = $_SERVER['QUERY_STRING'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.othersite.com/post2hostedscript.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
curl_exec($ch);
curl_close($ch);

For a list of options that can be used with curl, you can take a look at the page of curl_setopt.


Here, you'll have to use, at least :

  • CURLOPT_POST : as you want to send a POST request, and not a GET
  • CURLOPT_RETURNTRANSFER : depending on whether you want curl_exec to return the result of the request, or to just output it.
  • CURLOPT_POSTFIELDS : The data that will be posted -- i.e. what you have in the query string of your incoming request.

And note that the response from the POST request might include some interesting HTTP header -- if needed, you'll have to fetch them (see the CURLOPT_HEADER option), and re-send the interesting ones in your own response (see the header function).

Pascal MARTIN
+1 note though that GET data should not exceed 1024 bytes in size, and file uploads are impossible.
Pekka
@Pekka : right about file uploads, and a limit on the size of GET *(Even though I would have said the limit is higher than 1k ; I would have said 2k on some browsers, and 32k on some others ? )*
Pascal MARTIN
dbemerlin
@Pascal I had saved 1k as a general limit in my head long ago, but it could be 2k.
Pekka
@dbemerlin good info!
Pekka
@dbemerlin : thanks for the link :-)
Pascal MARTIN
i dont need upload etc. it's only a simple search-like query. the response should come directly from the hosted script. more like redirecting rather than proxying
DennyHalim.com
A: 

You might consider replacing all instances of $_POST in the old script to $_REQUEST, which will result in it accepting both GET and POST alike.

SF.