經過curl模擬post提交php
php方式:html
<?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
yii
<?php print_r($_POST); ?>
命令行方式:curl
curl -d "foo=bar" "http://localhost/post_output.php" post
經過curl模擬上傳文件url
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.phpcode
<?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命令行方式:htm
curl -F "file=@d:/wamp/www/test.zip" http://localhost/upload_output.phpip
經過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']; ?>
待完成。。。