PHP cURL: GET & POST
PHP cURL : Introduction | Basics | Authentication | SSL | GET & POST | GET POST SSL AUTH | CLI Set-up | CLI DtDNS Updater 1 | CLI DtDNS Updater 2
|
|
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.
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:
|
This page and server (curl_2) simulates a form on a remote server. Test:
Method GET Name = Password = Message = Method POST Name = Password = Message = |
The above processes GET and POST data retuning what was sent to the form.
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
|
Note: I have included print $str to show what the encoded strin looks like Test:
|
Results:
|
|
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
|
Note: I have included print $str to show what the encoded string looks like Test:
|
Results:
|
|
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.