Return to Snippet

Revision: 69497
at July 1, 2015 04:21 by pligor


Initial Code
mycurl.php:

<?php

$curl_connection = curl_init('http://george.pligor.com/testing/result.php');

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);	//seconds. Zero to wait indefinately

//curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");	//let's trick them by saying we are a mozilla agent ;)
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);	//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);	//FALSE to stop cURL from verifying the peer's certificate

curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true);	//TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set)

curl_setopt($curl_connection, CURLOPT_POST, false);	//TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms. 


$post_data["material"] = "1";
$post_data["Icc"] = "22";

foreach($post_data as $dataKey => $dataVal)
{
	$post_items[] = "$dataKey=$dataVal";
}

$post_string = implode("&",$post_items);

//$post_string = urlencode($post_string);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);	//This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value
//curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

$result = curl_exec($curl_connection);

//show information regarding the request
print "<pre>";
print_r(curl_getinfo($curl_connection));
print "</pre>";

print curl_errno($curl_connection) . '-' . curl_error($curl_connection);

curl_close($curl_connection);

?>

<p>
An ftasame edw ola ok!!
</p>

<pre>
<?php
	print_r($post_items);
	print "<br/>";
	print $post_string;
	print "<br/>";

	print $result;
?>
</pre>


result.php:

<p><b>For the get form we have:</b></p>
<?php
//if(isset($_GET['getform']))   //by hidden value field 
//{
	
	$material = $_GET["material"] ? "Copper" : "Aluminium";
	
	print "The material you have chosen is : $material";
	print "<br/><br/>";
	
	$Icc = $_GET["Icc"];
	print "The module's short circuit current is : $Icc";
	print "<br/><br/>";
//}
?>

<p><b>For the post form we have:</b></p>
<?php
//if(isset($_POST['postform']))   //by hidden value field
//{
	
	$chosenColor = $_POST["color"] ? "Vissini" : "Kokkino";
	
	print "The color you have chosen is : $chosenColor";
	print "<br/><br/>";
	
	$Voc = $_POST["Voc"];
	print "The open voltage of the module is : $Voc";
	print "<br/><br/>";
//}
?>

Initial URL


Initial Description
This source code contains two files:
1) result.php is a php webpage which expects a post request and renders some static results
2) mycurl.php uses curl to post programmatically some data to the result.php and after rendering the result is being return and saved inside a php variable. We later display inside the page this result

Note that in order to execute properly you should change the url inside curl_init inside the source code

Initial Title
Use curl in php to post to a php webpage programmatically and retrieve the result

Initial Tags
curl

Initial Language
PHP