DennyHalim.com
DennyHalim.com

Reputation: 143

php to translate GET request to POST request

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.

Upvotes: 1

Views: 671

Answers (3)

SF.
SF.

Reputation: 14077

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.

Upvotes: -1

Pascal MARTIN
Pascal MARTIN

Reputation: 401182

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).

Upvotes: 3

user253984
user253984

Reputation:

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

Upvotes: 1

Related Questions