在php中使用curl以编程方式发布到php网页并检索结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在php中使用curl以编程方式发布到php网页并检索结果相关的知识,希望对你有一定的参考价值。
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
mycurl.php: <?php //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 = urlencode($post_string); curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); //This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value //curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data); //show information regarding the request print "<pre>"; print "</pre>"; ?> <p> An ftasame edw ola ok!! </p> <pre> <?php 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/>"; //} ?>
以上是关于在php中使用curl以编程方式发布到php网页并检索结果的主要内容,如果未能解决你的问题,请参考以下文章