curl php w3c,PHP+curl,HTTP POST示例代码?

使用php curl_exec执行HTTPPOST的活生生的例子:

将其放入一个名为foobar.php的文件中:

$ch = curl_init();

$skipper = "luxury assault recreational vehicle";

$fields = array( 'penguins'=>$skipper, 'bestpony'=>'rainbowdash');

$postvars = '';

foreach($fields as $key=>$value) {

$postvars .= $key . "=" . $value . "&";

}

$url = "http://www.google.com";

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_POST, 1);                //0 for a get request

curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);

curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);

curl_setopt($ch,CURLOPT_TIMEOUT, 20);

$response = curl_exec($ch);

print "curl response is:" . $response;

curl_close ($ch);?>

然后使用命令运行它php foobar.php,它将这种输出转储到屏幕上:HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

charset=utf-8">

Title

A mountain of content...

因此,您在www.google.com上发了一个PHP帖子,并发送了一些数据。

如果服务器被编程来读取POST变量,那么它就可以决定在此基础上做一些不同的事情。

你可能感兴趣的:(curl,php,w3c)