通过curl模拟post提交
php方式:
<?php
$url = "http://localhost/post_output.php";
$post_data = array (
"foo" => "bar",
"query" => "Nettuts",
"action" => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 我们在POST数据哦!
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
?>
post_output.php
<?php
print_r($_POST);
?>
命令行方式:
curl -d "foo=bar" "http://localhost/post_output.php"
通过curl模拟上传文件
php方式:
<?php
$url = "http://localhost/upload_output.php";
$post_data = array (
"foo" => "bar",
// 要上传的本地文件地址
"file" => "@d:/wamp/www/test.zip"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
upload_output.php
<?php
if ($_FILES["file"]["error"] === 0){
$name = "upload/".time().$_FILES["file"]["name"];
if (file_exists($name))
{
echo $name . " already exists. ";
exit;
}else{
move_uploaded_file($_FILES["file"]["tmp_name"],$name);
echo "Stored in: " . $name;
}
}
curl命令行方式:
curl -F "file=@d:/wamp/www/test.zip" http://localhost/upload_output.php
通过curl设置header头信息
在一个HTTP请求中覆盖掉默认的HTTP头或者添加一个新的自定义头部字段
例如:
增加一个username参数
curl -H username:test123 -v http://www.myyii.dev/test/posts.html
php接收header头信息
<?php
print_r($_SERVER);
echo $_SERVER['HTTP_USERNAME'];
?>
待完成。。。
来源:oschina
链接:https://my.oschina.net/u/780509/blog/542743