PHP 在php中使用curl以编程方式发布到php网页并检索结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 在php中使用curl以编程方式发布到php网页并检索结果相关的知识,希望对你有一定的参考价值。

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/>";
//}
?>

以上是关于PHP 在php中使用curl以编程方式发布到php网页并检索结果的主要内容,如果未能解决你的问题,请参考以下文章

curl 不适用于 wamp 服务器的 php 5.4.3

在 PHP 中使用 Curl

更改 php(xampp) 中的 curl 扩展名?

如何以编程方式设置 Cron 作业?

PHP如何通过CURL上传文件

PHP 使用curl 执行远程服务器的一个shell脚本