• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Execute a HTTP POST Using PHP CURL ...

A customer recently brought to me a unique challenge. My customer wants information request form data to be collected in a database. Nothing new, right? Well, there's a hurdle — the information isn't going to be saved on the localhost database — it needs to be stored in a remote database that I cannot connect directly to.

View More Info

A customer recently brought to me a unique challenge. My customer wants information request form data to be collected in a database. Nothing new, right? Well, there is a hurdle — the information isn going to be saved on the localhost database — it needs to be stored in a remote database that I cannot connect directly to.

I thought about all of the possible solutions for solving this challenge and settled on this flow:

   1. User will submit the form, as usual.
   2. In the form processing PHP, I use CURL to execute a POST transmission to a PHP script on the customer is server.
   3. The remote script would do a MySQL INSERT query into the customer is private database.

This solution worked quite well so I thought I would share it with you. Here is how you execute a POST using the PHP CURL library.
view plaincopy to clipboardprint

   1. //extract data from the post 
   2. extract($_POST); 
   3.  
   4. //set POST variables 
   5. $url = http://domain.com/get-post.php; 
   6. $fields = array( 
   7.                         lname=>urlencode($last_name), 
   8.                         fname=>urlencode($first_name), 
   9.                         itle=>urlencode($title), 
  10.                         company=>urlencode($institution), 
  11.                         age=>urlencode($age), 
  12.                         email=>urlencode($email), 
  13.                         phone=>urlencode($phone) 
  14.                 ); 
  15.  
  16. //url-ify the data for the POST 
  17. foreach($fields as $key=>$value) { $fields_string .= $key.=.$value.&; } 
  18. rtrim($fields_string,&); 
  19.  
  20. //open connection 
  21. $ch = curl_init(); 
  22.  
  23. //set the url, number of POST vars, POST data 
  24. curl_setopt($ch,CURLOPT_URL,$url); 
  25. curl_setopt($ch,CURLOPT_POST,count($fields)); 
  26. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
  27.  
  28. //execute post 
  29. $result = curl_exec($ch); 
  30.  
  31. //close connection 
  32. curl_close($ch); 

source: davidwalsh

 View Full Story.
Posted at 08:21:00 am | Permalink | Posted in PHP  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Recent Stuff

Using ASP.NET AJAX JSON Asynchronous Web Services

Growl for Windows and a Web Notification API

Learn AJAX from Scratch - Part II

Learn AJAX from Scratch - Part I

PHP AJAX CHAT, Bug fixes - including weird mozilla bug

Learn PHP From Scratch


Our Partners

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines


Search


Topics

  • .Net (96)
  • Articles (76)
  • Bookmarking (35)
  • Calendar (18)
  • Chat (38)
  • ColdFusion (3)
  • CSS (30)
  • Email (23)
  • Flash (13)
  • Games (6)
  • Google (17)
  • Html (6)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (34)
  • Javascript (146)
  • JSON (16)
  • Perl (2)
  • PHP (82)
  • Presentation (19)
  • Python (3)
  • Resources (1)
  • RSS (1)
  • Ruby (7)
  • Storage (4)
  • Toolkits (87)
  • Tutorials (190)
  • UI (11)
  • Utilities (161)
  • Web2.0 (13)
  • XmlHttpRequest (18)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange