Home > PHP > Using curl with PHP Tutorial

Using curl with PHP Tutorial

February 3rd, 2009

Recently while developing a database backed php website, the client demanded to have all forms on one site say www.client-forms-website.com, and all the data to be submitted and stored on www.client-database-website.com.

Curl was the ultimate solution. curl is the client URL function library. PHP supports it through libcurl. To enable support for libcurl when installing PHP add –with-curl=[location of curl libraries] to the configure statement before compiling. The curl package must be installed prior to installing PHP. Most major functions required when connecting to remote web servers are included in curl, including POST and GET form posting, SSL support, HTTP authentication, session and cookie handling.

Leaving out all the fancy stuff, this is what I implemented:

On www.client-forms-website.com:

Created a file “receive-form-data.php” with following code.

<?php

// Move posted data into variables.

$firstName= @$_POST[firstName];
$surName= @$_POST[surName];
$email= @$_POST[email];

//The $curlPost variable is being used to store the POST data curl will use. When forming the $curlPost variable which will be used by curl_setopt later be sure to urlencode your data prior to passing it to curl_setopt.

$curlPost = “firstname=”.urlencode($firstName).”&surname=”. urlencode($surName).”&email=”. urlencode($email).”&submitted=true”;
$ch = curl_init();
//set the handle of the curl session to $ch
curl_setopt($ch, CURLOPT_URL, ‘http:// www.client-database-website.com/process-posted-data.php’); //set the URL of the page to pass data.
curl_setopt($ch, CURLOPT_HEADER, 0); //sets whether or not the server response header should be returned, 0 means no header.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //by default curl will display the response straight to the browser as the script is executed. To counter this we enabled the CURLOPT_RETURNTRANSFER option.
curl_setopt($ch, CURLOPT_POST, 1); //tell curl to send the form response via the POST method.
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); //option used to store the POST data.
$data = curl_exec($ch); // $data stores data returned from the remote server.
curl_close($ch);

if ($data==1) header(“location: http:// www.client-forms-website.com /thankyou.html“);

else if ($data==0) header(“location: http:// www.client-forms-website.com /error.html“);

?>

On www.client- database -website.com:

Created a file “process-posted-data.php’” with following code.

<?php

$firstName= @$_POST[firstName]; $surName= @$_POST[surName]; $email= @$_POST[email];

/* Function/lines of code to Store Data go here.*/

If (success) echo 1;

else echo 0;

?>

This is working great. Have fun.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit This Post Post to StumbleUpon Stumble This Post

PHP , ,

  1. David
    February 5th, 2009 at 13:51 | #1

    saved my day, keep it up.

  2. Rehan
    February 10th, 2009 at 08:44 | #2

    great tips.. i am giving them a go

  1. No trackbacks yet.

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.