PHP cURL: GET & POST

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MPG UniCenter

UniServer 5-Nano
PHP cURL.

cURL GET and POST

One powerful feature of cURL is its ability to GET and POST data to online forms.

This page provides examples showing how to do this using our test server to simulate a server on the Internet.

Preperation

Disable Authentication

If you have been following this tutorial we need to turn off authentication on our test server curl_2

Edit file C:\curl_2\UniServer\www\.htaccess

Change these four lines:

AuthName "Uniform Server - Server Access"
AuthType Basic
AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
Require valid-user

To:

#AuthName "Uniform Server - Server Access"
#AuthType Basic
#AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd
#Require valid-user

A name and password is no longer required.

Top

Form Response script

Testing GET and POST requires a script that produces a response:

Create a new text file in folder C:\curl_2\UniServer\www named remote_form.php with the following content:

<?php
 print "<p><b>Method GET</b><br />";
 if(isset($_GET['Name']))       $Name     = $_GET['Name'];
 if(isset($_GET['Password']))   $Password = $_GET['Password'];
 if(isset($_GET['Message']))    $Message  = $_GET['Message'];
 print "Name = $Name<br />";
 print "Password = $Password<br />";
 print "Message = $Message</p>";

 print "<p><b>Method POST</b><br />";
 if(isset($_POST['Name']))       $Name     = $_POST['Name'];
 if(isset($_POST['Password']))   $Password = $_POST['Password'];
 if(isset($_POST['Message']))    $Message  = $_POST['Message'];
 print "Name = $Name<br />";
 print "Password = $Password<br />";
 print "Message = $Message</p>";
?>

This page and server (curl_2) simulates a form on a remote server.

Test:

  • Type the following in to a browser: Enter http://localhost:82/remote_form.php
  • Result: Following displayed:
Method GET
Name =
Password =
Message =

Method POST
Name =
Password =
Message = 

The above processes GET and POST data retuning what was sent to the form.

Top

Example 10 - cURL GET

Curl does not specifically include a function for GET hence you just tack the data onto the end of the URL. Data has the following format:

  • http://domain.com?name=value&name=value&name=value
  • Data starts with a question mark followed by a series of name=value pairs each separated by an ampersand
  • A value can contain any printable character including special character such as (? & = space) because of this they require URL encoding using function urlencode("value")

Create a new text file in folder C:\curl_1\UniServer\www and name it test10.php add the following content

<?php
$name_val     = urlencode('MPG RIC');
$password_val = urlencode('fred 123');
$message_val  = urlencode('This is a test & = ?');

$str= "?Name=".$name_val."&Password=".$password_val."&Message=".$message_val;

print $str;

 $ch=curl_init();
 curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_form.php'.$str);
 curl_exec($ch);
 curl_close($ch);
?>

 

  • First three lines URL encode the values we wish to send.
  • Full string $str is assembled into correct format name value pairs.
  • This string is concatenated with the URL (tacked onto the end)

Note: I have included print $str to show what the encoded strin looks like

Test:

  • Run both servers
  • Type http://localhost/test10.php into your browser
  • Result: Shown below

Results:

?Name=MPG+RIC&Password=fred+123&Message=This+is+a+test+%26+%3D+%3F

Method GET
Name = MPG RIC
Password = fred 123
Message = This is a test & = ?

Method POST
Name = MPG RIC
Password = fred 123
Message = This is a test & = ?

 

  • Note 1: The encoding of & = ? as %26+%3D+%3F (+ is a special character if we used that it would have been encoded)


  • Note 2: Although we used GET to send data for some reason PHP has mapped the variables in both GET and POST super-globals (I think this is incorrect!).

Top

Example 11 - cURL POST

Curl uses two functions to implement posting data.

  • curl_setopt ($ch, CURLOPT_POST, 1) - Informs Curl to switch from GET (default) to POST
  • curl_setopt ($ch, CURLOPT_POSTFIELDS, $str) - Informs Curl the string we want to post


  • String $str has the same format as GET with one exception a question mark is not required. Data is arranged in a series of name=value pairs each separated by an ampersand.
  • A value can contain any printable character including special character such as (? & = +) because of this they require URL encoding using function urlencode("value")

Create a new text file in folder C:\curl_1\UniServer\www and name it test11.php add the following content

<?php
$name_val     = urlencode('MPG RIC');
$password_val = urlencode('fred 123');
$message_val  = urlencode('This is a test & = +');

$str= "Name=".$name_val."&Password=".$password_val."&Message=".$message_val;

print $str;

 $ch=curl_init();
 curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_form.php');
 curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, $str);

 curl_exec($ch);
 curl_close($ch);
?>

 

  • First three lines URL encode the values we wish to send.
  • Full string $str is assembled into correct format name value pairs.
  • CURLOPT_POST Switches to POST operation
  • CURLOPT_POSTFIELDS Informs Curl we are using a value of $str
  • This string has been URL encoded.

Note: I have included print $str to show what the encoded string looks like

Test:

  • Run both servers
  • Type http://localhost/test11.php into your browser
  • Result: Shown below

Results:

Name=MPG+RIC&Password=fred+123&Message=This+is+a+test+%26+%3D+%2B

Method GET
Name =
Password =
Message =

Method POST
Name = MPG RIC
Password = fred 123
Message = This is a test & = +

 

  • Note 1: The encoding of & = + as %26+%3D+%2B (+ is a special character hence encoded as %2B)


  • Note 2: Results in POST section as expected (no values in GET which is correct).

Top

Summary

Sending form data using either GET or POST with Curle again is very easy.

The next page covers sending data using GET or POST overs SSL to an authentication server.

Top