• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Submitting a form post with PHP and CURL

Submitting a form post with PHP and CURL

The PHP CURL functions use the libcurl library to allow you to connect to various servers and different protocols. This post shows how to make an HTTP POST with PHP and CURL passing several form fields. This can be useful for testing your own web forms, connecting to APIs that require POST data and so on.

The PHP Code

The following PHP code example posts to http://www.example.com/path/to/form sending three sets of values.

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/path/to/form");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'foo' => 'foo foo foo',
    'bar' => 'bar bar bar',
    'baz' => 'baz baz baz'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

CURLOPT_RETURNTRANSFER

The "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);" line is not actually necessary but it means the HTML from the web page returned goes into the $output variable rather than echoed out to standard output.

CURLOPT_POST

The "curl_setopt($ch, CURLOPT_POST, true);" line tells CURL that this is POST instead of the default GET.’

CURLOPT_POSTFIELDS

The $data array contains the POST field values. The first of these has the name ‘foo’ and value ‘foo foo foo’; the equivilent HTML form value for this might be something like <input type=’hidden’ name=’foo’ value=’foo foo foo’ />

The post field values are then set with the "curl_setopt($ch, CURLOPT_POSTFIELDS, $data);" line. $data can optionally be a string formatted in the same way a GET string is (e.g. foo=foo+foo+foo&bar=bar+bar+bar etc) but I find it easier to use an array.

curl_getinfo()

The "$info = curl_getinfo($ch);" is also not required but returns an array of useful data about the request. Example output from the above would look something like so:

Array
(
    [url] => http://www.example.com/path/to/form
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 516
    [request_size] => 197
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.256708
    [namelookup_time] => 0.672754
    [connect_time] => 0.899986
    [pretransfer_time] => 0.900012
    [size_upload] => 240
    [size_download] => 18717
    [speed_download] => 8293
    [speed_upload] => 106
    [download_content_length] => 0
    [upload_content_length] => 240
    [starttransfer_time] => 1.12957
    [redirect_time] => 0
)

Getting the http_code from the information can be useful so you know if it successfully connected to the page before parsing any of the return data.

Check Out These Related posts:

  1. Get unique array values with PHP
  2. MAMP PHP cURL and SSL
  3. PHP class for using the Google Analytics API
  4. Post a form to a popup window with Javascript and jQuery

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap