Thursday, May 3, 2007

PHP and CURL (make post between two sites more secure)

We can also use CURL to post data to a form. The default method of sending form data with CURL is in GET, but in this example we will use POST to submit a search term to an imaginary form.

[php] // Setup a string with the form parameters in it
$strParameters = "query=dog";

// Initialize the CURL library
$cURL = curl_init($cURL);

// Set the URL to execute
curl_setopt($cCURL, CURLOPT_URL, "http://www.mywebserver.com/mysearch.php");

// Set options
curl_setopt($cCURL, CURLOPT_HEADER, 1);
curl_setopt($cCURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cCURL, CURLOPT_POST, 1);
curl_setopt($cCURL, CURLOPT_POSTFIELDS, $strParameters);

// Execute, saving results in a variable
$strPage = curl_exec();

// Close CURL resource
curl_close($cURL);

// This will print out the HTML contents
echo($strPage);

?>[/php]

No comments: