[thelist] Submitting Request.ServerVariables("REQUEST_METHOD") in the URL

Simon Willison cs1spw at bath.ac.uk
Sat Oct 4 13:48:23 CDT 2003


Rodrigo Fonseca wrote:
> A quick example to help you out:
> 
> $ch = curl_init(); // init
> // Next line is the page that will get the form post
> curl_setopt($ch, CURLOPT_URL, "http://www.domain_here.com");
> // do not print any errors
> curl_setopt($ch, CURLOPT_FAILONERROR, 1);
> // if the page requires a valid referer use this:
> curl_setopt($ch, CURLOPT_REFERER, "http://www.referer_page_here");
> // follow the destination (URL) location
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
> // timeout - in seconds
> curl_setopt($ch, CURLOPT_TIMEOUT, 20);
> // return the transfer instead of printing it out
> curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
> // perform a regular HTTP post
> curl_setopt($ch, CURLOPT_POST, 1);
> // the fields you want to post (build them as a normal query string)
> curl_setopt($ch, CURLOPT_POSTFIELDS,"var1=value&var2=value2&var3=test");
> // exec the transfer or go to an error page
> $result = curl_exec ($ch) or die(header("Location: ./error.php"));
> // close the connection
> curl_close ($ch);
> 
> The $result variable will then get the content of the transfer, it can
> be a full response web page or just an array with values or a thank you
> message, depending ou how the "http://www.domain_here.com" page is
> written.

Or the equivalent with HttpClient:

$result = HttpClient::quickPost('http://www.domain_here.com/', array(
     'var1' => 'value',
     'var2' => 'value2,
     'var3' => 'test'
));

That's assuming you don't need to set the referer. If you do, it's a 
couple of extra lines:

$client = new HttpClient('www.domain_here.com');
$client->referer = 'http://www.referer_page_here';
$client->post('/', array(
     'var1' => 'value',
     'var2' => 'value2,
     'var3' => 'test'
));
$result = $client->getContent();

I've always found cURL a bit verbose for my tastes.

Cheers,

Simon Willison
<pimp>
http://scripts.incutio.com/httpclient/
</pimp>



More information about the thelist mailing list