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


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. mycurl.php:
  2.  
  3. <?php
  4.  
  5. $curl_connection = curl_init('http://george.pligor.com/testing/result.php');
  6.  
  7. curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); //seconds. Zero to wait indefinately
  8.  
  9. //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 ;)
  10. curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  11.  
  12. 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.
  13.  
  14. curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //FALSE to stop cURL from verifying the peer's certificate
  15.  
  16. 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)
  17.  
  18. 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.
  19.  
  20.  
  21. $post_data["material"] = "1";
  22. $post_data["Icc"] = "22";
  23.  
  24. foreach($post_data as $dataKey => $dataVal)
  25. {
  26. $post_items[] = "$dataKey=$dataVal";
  27. }
  28.  
  29. $post_string = implode("&",$post_items);
  30.  
  31. //$post_string = urlencode($post_string);
  32.  
  33. 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
  34. //curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);
  35.  
  36. $result = curl_exec($curl_connection);
  37.  
  38. //show information regarding the request
  39. print "<pre>";
  40. print_r(curl_getinfo($curl_connection));
  41. print "</pre>";
  42.  
  43. print curl_errno($curl_connection) . '-' . curl_error($curl_connection);
  44.  
  45. curl_close($curl_connection);
  46.  
  47. ?>
  48.  
  49. <p>
  50. An ftasame edw ola ok!!
  51. </p>
  52.  
  53. <pre>
  54. <?php
  55. print_r($post_items);
  56. print "<br/>";
  57. print $post_string;
  58. print "<br/>";
  59.  
  60. print $result;
  61. ?>
  62. </pre>
  63.  
  64.  
  65. result.php:
  66.  
  67. <p><b>For the get form we have:</b></p>
  68. <?php
  69. //if(isset($_GET['getform'])) //by hidden value field
  70. //{
  71.  
  72. $material = $_GET["material"] ? "Copper" : "Aluminium";
  73.  
  74. print "The material you have chosen is : $material";
  75. print "<br/><br/>";
  76.  
  77. $Icc = $_GET["Icc"];
  78. print "The module's short circuit current is : $Icc";
  79. print "<br/><br/>";
  80. //}
  81. ?>
  82.  
  83. <p><b>For the post form we have:</b></p>
  84. <?php
  85. //if(isset($_POST['postform'])) //by hidden value field
  86. //{
  87.  
  88. $chosenColor = $_POST["color"] ? "Vissini" : "Kokkino";
  89.  
  90. print "The color you have chosen is : $chosenColor";
  91. print "<br/><br/>";
  92.  
  93. $Voc = $_POST["Voc"];
  94. print "The open voltage of the module is : $Voc";
  95. print "<br/><br/>";
  96. //}
  97. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.