https://wiki.uniformserver.com/index.php?title=PHP_cURL:_GET_%26_POST&feed=atom&action=historyPHP cURL: GET & POST - Revision history2024-03-29T15:43:21ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=PHP_cURL:_GET_%26_POST&diff=3886&oldid=prevRic: New page: {{Nav 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 te...2009-09-11T20:02:24Z<p>New page: {{Nav 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 te...</p>
<p><b>New page</b></p><div>{{Nav PHP cURL}}<br />
'''cURL GET and POST'''<br />
<br />
One powerful feature of cURL is its ability to GET and POST data to online forms.<br />
<br />
This page provides examples showing how to do this using our test server to simulate a server on the Internet.<br />
== Preperation ==<br />
<br />
=== Disable Authentication ===<br />
If you have been following this tutorial we need to turn off authentication on our test server curl_2<br />
<br />
Edit file C:\curl_2\UniServer\www\'''.htaccess'''<br />
<br />
Change these four lines:<br />
<pre><br />
AuthName "Uniform Server - Server Access"<br />
AuthType Basic<br />
AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd<br />
Require valid-user<br />
</pre> <br />
To:<br />
<pre><br />
#AuthName "Uniform Server - Server Access"<br />
#AuthType Basic<br />
#AuthUserFile C:/curl_2/UniServer/htpasswd/www/.htpasswd<br />
#Require valid-user<br />
</pre> <br />
A name and password is no longer required.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Form Response script ===<br />
Testing GET and POST requires a script that produces a response:<br />
<br />
Create a new text file in folder C:\curl_2\UniServer\'''www''' named '''remote_form.php''' with the following content:<br />
{|<br />
|-<br />
|<br />
{|<br />
|-<br />
|<br />
<pre><br />
<?php<br />
print "<p><b>Method GET</b><br />";<br />
if(isset($_GET['Name'])) $Name = $_GET['Name'];<br />
if(isset($_GET['Password'])) $Password = $_GET['Password'];<br />
if(isset($_GET['Message'])) $Message = $_GET['Message'];<br />
print "Name = $Name<br />";<br />
print "Password = $Password<br />";<br />
print "Message = $Message</p>";<br />
<br />
print "<p><b>Method POST</b><br />";<br />
if(isset($_POST['Name'])) $Name = $_POST['Name'];<br />
if(isset($_POST['Password'])) $Password = $_POST['Password'];<br />
if(isset($_POST['Message'])) $Message = $_POST['Message'];<br />
print "Name = $Name<br />";<br />
print "Password = $Password<br />";<br />
print "Message = $Message</p>";<br />
?><br />
</pre><br />
|}<br />
|<br />
This page and server (curl_2) simulates a form on a remote server.<br />
<br />
'''''Test'':'''<br />
* Type the following in to a browser: Enter <nowiki>http:</nowiki>//localhost:82/remote_form.php<br />
* Result: Following displayed:<br />
<pre><br />
Method GET<br />
Name =<br />
Password =<br />
Message =<br />
<br />
Method POST<br />
Name =<br />
Password =<br />
Message = <br />
</pre><br />
|}<br />
The above processes GET and POST data retuning what was sent to the form. <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Example 10 - cURL GET ==<br />
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:<br />
<br />
* <nowiki>http:</nowiki>//domain.com'''?name=value&name=value&name=value'''<br />
* Data starts with a question mark followed by a series of name=value pairs each separated by an ampersand<br />
* A value can contain any printable character including special character such as (? & = space) because of this they require URL encoding using function '''urlencode("value")'''<br />
<br />
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test10.php''' add the following content<br />
{|<br />
|-valign="top"<br />
|<br />
{|cellspacing="6"<br />
|-<br />
|<br />
<pre><br />
<?php<br />
$name_val = urlencode('MPG RIC');<br />
$password_val = urlencode('fred 123');<br />
$message_val = urlencode('This is a test & = ?');<br />
<br />
$str= "?Name=".$name_val."&Password=".$password_val."&Message=".$message_val;<br />
<br />
print $str;<br />
<br />
$ch=curl_init();<br />
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_form.php'.$str);<br />
curl_exec($ch);<br />
curl_close($ch);<br />
?><br />
</pre><br />
|}<br />
|<br />
&nbsp;<br /><br />
* First three lines URL encode the values we wish to send.<br />
* Full string '''$str''' is assembled into correct format name value pairs.<br />
* This string is concatenated with the URL (tacked onto the end) <br />
<br />
'''''Note'':''' I have included '''print $str''' to show what the encoded strin looks like<br />
<br />
'''''Test'':'''<br />
* Run both servers<br />
* Type '''<nowiki>http:</nowiki>//localhost/test10.php''' into your browser<br />
* Result: Shown below<br />
|}<br />
'''''Results'':'''<br />
{|<br />
|-valign="top"<br />
|<br />
{|<br />
|-<br />
|<br />
<pre><br />
?Name=MPG+RIC&Password=fred+123&Message=This+is+a+test+%26+%3D+%3F<br />
<br />
Method GET<br />
Name = MPG RIC<br />
Password = fred 123<br />
Message = This is a test & = ?<br />
<br />
Method POST<br />
Name = MPG RIC<br />
Password = fred 123<br />
Message = This is a test & = ?<br />
</pre><br />
|}<br />
|<br />
&nbsp;<br /><br />
* '''''Note 1'':''' The encoding of & = ? as %26+%3D+%3F (+ is a special character if we used that it would have been encoded)<br />
<br />
<br />
* '''''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!).<br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Example 11 - cURL POST ==<br />
Curl uses two functions to implement posting data.<br />
<br />
* '''curl_setopt ($ch, CURLOPT_POST, 1)''' - Informs Curl to switch from GET (default) to POST<br />
* '''curl_setopt ($ch, CURLOPT_POSTFIELDS, $str)''' - Informs Curl the string we want to post <br />
<br />
<br />
* 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.<br />
* A value can contain any printable character including special character such as (? & = +) because of this they require URL encoding using function '''urlencode("value")'''<br />
<br />
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test11.php''' add the following content<br />
{|<br />
|-valign="top"<br />
|<br />
{|cellspacing="6"<br />
|-<br />
|<br />
<pre><br />
<?php<br />
$name_val = urlencode('MPG RIC');<br />
$password_val = urlencode('fred 123');<br />
$message_val = urlencode('This is a test & = +');<br />
<br />
$str= "Name=".$name_val."&Password=".$password_val."&Message=".$message_val;<br />
<br />
print $str;<br />
<br />
$ch=curl_init();<br />
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_form.php');<br />
curl_setopt ($ch, CURLOPT_POST, 1);<br />
curl_setopt ($ch, CURLOPT_POSTFIELDS, $str);<br />
<br />
curl_exec($ch);<br />
curl_close($ch);<br />
?><br />
</pre><br />
|}<br />
|<br />
&nbsp;<br /><br />
* First three lines URL encode the values we wish to send.<br />
* Full string '''$str''' is assembled into correct format name value pairs.<br />
* '''CURLOPT_POST''' Switches to POST operation<br />
* '''CURLOPT_POSTFIELDS''' Informs Curl we are using a value of $str<br />
* This string has been URL encoded. <br />
<br />
'''''Note'':''' I have included '''print $str''' to show what the encoded string looks like<br />
<br />
'''''Test'':'''<br />
* Run both servers<br />
* Type '''<nowiki>http:</nowiki>//localhost/test11.php''' into your browser<br />
* Result: Shown below<br />
|}<br />
'''''Results'':'''<br />
{|<br />
|-valign="top"<br />
|<br />
{|<br />
|-<br />
|<br />
<pre><br />
Name=MPG+RIC&Password=fred+123&Message=This+is+a+test+%26+%3D+%2B<br />
<br />
Method GET<br />
Name =<br />
Password =<br />
Message =<br />
<br />
Method POST<br />
Name = MPG RIC<br />
Password = fred 123<br />
Message = This is a test & = +<br />
</pre><br />
|}<br />
|<br />
&nbsp;<br /><br />
* '''''Note 1'':''' The encoding of & = + as %26+%3D+%2B (+ is a special character hence encoded as %2B)<br />
<br />
<br />
* '''''Note 2'':''' Results in POST section as expected (no values in GET which is correct). <br />
|}<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
Sending form data using either GET or POST with Curle again is very easy.<br />
<br />
The [[PHP cURL: GET POST SSL AUTH | '''next page''']] covers sending data using GET or POST overs SSL to an authentication server.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric