很多站长朋友们都不太清楚phppost使用,今天小编就来给大家整理phppost使用,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php实现模拟post请求用法实例 2、 PHP的POST怎么用? 3、 关于PHP中POST传递参数问题 4、 如何用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=array()){//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;
}
}
$url='http://******/con/Inter.php';
$data=Request::post($url,array('api'=>'tag_list'));
$data2=Request::post2($url,array('api'=>'tag_list'));
echo
$data;
希望本文所述对大家的php程序设计有所帮助。
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传递参数问题将数据转换成 json 格式的字符串, 并通过 CURL 的 POST 的形式传递参数给服务端, 但是在服务端无法用 $_POST 获取到数据。后台用 $_POST 获取到的信息为空, 但是可以通过 $post = file_get_contents("php://input") 获取到请求的相关信息。
Coentent-Type 的值为 application/x-www-data-urlencode 和 multipart/form-data 时, php才会将http请求数据包中的数据填进 $_POST 。
如果 POST 的原始数据是一维数组或拼接的标准格式的键值对字符串,那么可以用 $_POST 来获取。
如果要通过 file_get_contents 获取,这种情况下可以发送 json 字符串,用 json_encode 编码转换一下,或者使用 http_build_query 。
1、 区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
2、 accept 和 content-Type区别
3、 Http Header里的Content-Type
如何用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);
关于phppost使用的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。