导读:今天编程笔记来给各位分享关于php如何post的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、怎么用PHP发送POST请求
2、PHP的POST怎么用?
3、php怎么以post方式发送数据
4、求助PHP如何POST提交数据
5、php 怎么POST获取数据?
怎么用PHP发送POST请求
PHP发送POST请求的三种方式
class Request{
public static function post($url, $post_data = '', $timeout = 5){//curl
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
if($post_data != ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
public static function post2($url, $data){//file_get_content
$postdata = http_build_query(
$data
);
$opts = array('http' =
array(
'method' = 'POST',
'header' = 'Content-type: application/x-www-form-urlencoded',
'content' = $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
public static function post3($host,$path,$query,$others=''){//fsocket
$post="POST $path HTTP/1.1\r\nHost: $host\r\n";
$post.="Content-type: application/x-www-form-";
$post.="urlencoded\r\n${others}";
$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";
$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";
$h=fsockopen($host,80);
fwrite($h,$post);
for($a=0,$r='';!$a;){
$b=fread($h,8192);
$r.=$b;
$a=(($b=='')?1:0);
}
fclose($h);
return $r;
}
}
![](https://img.php1.cn/3cd4a/1eebe/cd5/fb32005f2115b419.webp)
PHP的POST怎么用?
表单提交一般有两种方式GET、POST。
POST方式的用法如下
代码例如:文件为index.php
html代码
form name="biaodan" method="post" action="index.php?action=ok"
姓名:input type="text" name="name" value=""
br
性别:input type="text" name="sex" value=""
br
input type="submit" value="提交"
/form
php代码
?php
if(isset($_GET['action']) $_GET['action'] == 'ok'){
$name = $_POST['name'];
$sex = $_POST['sex'];
echo '姓名为:'.$name;
echo 'br';
echo '性别为:'.$sex;
}
?
php怎么以post方式发送数据
:用PHP向服务器发送HTTP的POST请求,代码如下:?php/***发送post请求*@paramstring$url请求地址*@paramarray$post_datapost键值对数据*@returnstring*/.
求助PHP如何POST提交数据
用PHP向服务器发送HTTP的POST请求,代码如下:
?php
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' = array(
'method' = 'POST',
'header' = 'Content-type:application/x-www-form-urlencoded',
'content' = $postdata,
'timeout' = 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
使用的时候直接调用上面定义的send_post方法:
$post_data = array(
'username' = 'username',
'password' = 'password'
);
send_post('网址', $post_data);
php 怎么POST获取数据?
方法1、最常见的方法是:$_POST['fieldname'];
说明:只能接收Content-Type:
application/x-www-form-urlencoded提交的数据
解释:也就是表单POST过来的数据
方法2、file_get_contents("php://input");
说明:
允许读取
POST
的
原始数据
。
和
$HTTP_RAW_POST_DATA
比起来,它给内存带来的压力较小,并且不需要任何特殊的
php.ini
设置。
php://input
不能用于
enctype="multipart/form-data"。
解释:
对于未指定
Content-Type
的POST数据,则可以使用file_get_contents(“php://input”);来获取原始数据。
事实上,用PHP接收POST的任何数据都可以使用本方法。而不用考虑Content-Type,包括
二进制文件
流也可以。
所以用方法二是最保险的方法
方法3、$GLOBALS['HTTP_RAW_POST_DATA'];
说明:
总是产生
$HTTP_RAW_POST_DATA
变量包含有原始的
POST
数据。
此变量仅在碰到未识别
MIME
类型的数据时产生。
$HTTP_RAW_POST_DATA
对于
enctype="multipart/form-data"
表单数据不可用
如果post过来的数据不是PHP能够识别的,可以用
$GLOBALS['HTTP_RAW_POST_DATA']来接收,
比如
text/xml
或者
soap
等等
解释:
$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST过来的原始数据。
$_POST或
$_REQUEST
存放的是
PHP以key=value的形式格式化以后的数据。
但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于centent-Type的设置,即POST数据时
必须显式示指明Content-Type:
application/x-www-form-urlencoded,POST的数据才会存放到
$GLOBALS['HTTP_RAW_POST_DATA']中
结语:以上就是编程笔记为大家整理的关于php如何post的相关内容解答汇总了,希望对您有所帮助!如果解决了您的问题欢迎分享给更多关注此问题的朋友喔~