Forum Moderators: coopster

Message Too Old, No Replies

Web Tool for Converting GET vars to POST vars

Useful for testing

         

trillianjedi

6:52 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've found myself using API's quite a lot (including my own webapps) and I don't know about you guys, but sometimes it's a lot more simple to just shove a URL in a browser to test the output. Otherwise I have to fire up Charles on my Mac to do the form submission. But my webapps, like most, use POST VARS and not GET VARS.

So I built a little PHP web page tool for taking a GET VAR constructed URL, turn into POST vars and then submit the form for you.

This means you can just shove a regular old URL into it and do the post.

Saves me a lot of time - so posted here as public domain code and I hope someone else finds it useful.

You'll need the cURL library to use it.

Usage:-

Construct a URL with the POST vars you want submitted appended to the FORM POST URL in the same way you would GET VARS, pop it in the text box and hit the button.

Example : http://example.com/myForm.php?var1=hello&var2=World


<?php

// getToPost.php - converts a GET encoded URL to a POST
// To remove the pain in the **** that testing POST forms are
// Thrown together by TrillianJedi and published on WebmasterWorld.com, 28/03/2010. Public Domain, E&OE....
// Requires cURL library
//
// Edit the "<WHEREVER_YOU_PUT_THIS>" bit to use and upload to a cURL enabled PHP webserver

$uri = $_POST['data'];

if ($uri == "") {

echo "
<html>
<head>
<title>Post Me</title>
</head>

<body>

<div style='text-align:center; padding: 50px;'>
<form action='http://<WHEREVER_YOU_PUT_THIS>/getToPost.php' method='post'>
URL : <input type='text' name='data' style='width: 550px'>
<INPUT type='submit' value='Post It For Me'>
<form>
</div>

</body>

</html>
";

} else {

// Do the post

// Get the POST url

$postArray = explode("?", $uri);

$url = $postArray[0];

$args = $postArray[1];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);

// If this takes longer than 5 seconds, we probably have an issue....
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);

$posted = curl_exec($ch);
curl_close($ch);

// Quick and dirty output check - if this is XML like a lot of XML-RPC webservices are, make it render nicely in browser

if (strstr($posted, "<?xml")) {
header ("content-type: text/xml");
}

echo $posted;
}

?>

mack

7:52 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Very nice idea!

I have a bad habit of cheating by replacing $_post with $_request when debugging so I can just pass the values via the address bar. or as post variables.

Mack.

trillianjedi

8:02 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I share that bad habit Mack ;)

But I don't tend to let that through to production code (which sometimes you still need to quick/dirty test) - and if it's a third party webservice API like Twitter or Facebook they often disallow it too.

I'm already at v1.1 of this actually - I changed the strstr search as 99% of API's I use return something other than HTML. So instead I put in a radio button to determine how you want the output (HTML or Plan-text/XML).

I'll post up revised code later. Might tweak it some more.

All ideas/code improvement welcome. I find this one so useful.

Anyango

8:44 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_POST=$_GET; //Testing

Isnt it as simple as that, or am i too tired to see what the post is trying to say?

trillianjedi

8:53 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The latter, re-read in the morning ;)

Or, solve the problem of how you put that code on Twitters servers, for example.

EG, bear in mind this is interfacing with a webapp on another server. It is not, of itself, a webapp.

mack

9:16 pm on Mar 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The more I look at this, the more I wish I had seen it last week lol. It would have saved me a headache!

Mack.