php发送post数据 php发送post请求api

php怎么以post方式发送数据:用PHP向服务器发送HTTP的POST请求,代码如下:?php/***发送post请求*@paramstring$url请求地址*@paramarray$post_datapost键值对数据*@returnstring*/.
PHP怎么发送POST数据放到form里面
form action='abc.php' method='post'
input type='hidden' value='https://www.04ip.com/post/6d5ecc35ad679c37c8f029f587ce3940' name='sign'
input type='hidden' value='https://www.04ip.com/post/MD5' name='sign_type'
input type='submit'
/form
点击那个submit按钮就可以了
关于PHP中POST传递参数问题将数据转换成 json 格式php发送post数据的字符串,并通过 CURL 的 POST 的形式传递参数给服务端, 但是在服务端无法用 $_POST 获取到数据 。后台用 $_POST 获取到的信息为空,但是可以通过 $post = file_get_contents("php://input") 获取到请求的相关信息 。
Coentent-Type 的值为 application/x-www-data-urlencode 和 multipart/form-data 时, php才会将http请求数据包中的数据填进 $_POST。
如果 POST 的原始数据是一维数组或拼接的标准格式的键值对字符串php发送post数据,那么可以用 $_POST 来获取 。
如果要通过 file_get_contents 获取php发送post数据,这种情况下可以发送 json 字符串php发送post数据,用 json_encode 编码转换一下php发送post数据,或者使用 http_build_query。
1、 区别 PHP 的 $_POST、$HTTP_RAW_POST_DATA 和 php://input
2、 accept 和 content-Type区别
3、 Http Header里的Content-Type
php发送get , post请求的几种方法POST方法比较适合用于发送一个保密php发送post数据的(比如信用卡号)或者比较大量的数据到服务器php发送post数据,但速度慢 。而Get方法会将所要传输的数据附在网址后面 , 然后一起送达服务器,因此传送的数据量就会受到限制且不安全,但是执行效率却比 Post方法好 。具体来说如下:
1、Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“”连接;Post是将表单中的数据放在form的数据体中,按照变量和值相对应的方式 , 传递到action所指向URL 。
2、Get是不安全的,因为在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到 。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前 。Post的所有操作对用户来说都是不可见的 。
3、Get传输的数据量?。?这主要是因为受URL长度限制;而Post可以传输大量的数据,所以在上传文件只能使用Post(当然还有一个原因 , 将在后面的提到) 。
4、Get限制Form表单的数据集的值必须为ASCII字符;而Post支持整个ISO10646字符集 。
5、Get是Form的默认方法 。
php 提交post数据的问题在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考 。
注意:curl函数在php中默认是不被支持的,如果需要使用curl函数我们需在改一改你的php.ini文件的设置,找到php_curl.dll去掉前面的";"就行了
例1
?php
$uri = "";
// 参数数组
$data = https://www.04ip.com/post/array (
'name' = 'tanteng'
// 'password' = 'password'
);
$ch = curl_init ();
// print_r($ch);
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );
print_r($return);
接受php页面远程服务器:
?php
if(isset($_POST['name'])){
if(!empty($_POST['name'])){
echo '您好,',$_POST['name'].'!';
}
}
?
例2
用CURL模拟POST请求抓取邮编与地址
完整代码: 代码如下
#!/usr/local/php/bin/php
?php
$runtime = new runtime ();
$runtime-start ();
$cookie_jar = tempnam('/tmp','cookie');
$filename = $argv[1];
$start_num= $argv[2];
$end_num= $argv[3];
for($i=$start_num; $i$end_num; $i){
$zip = sprintf('6s',$i);
$fields_post = array(
'postcode' = $zip,
'queryKind' = 2,
'reqCode' = 'gotoSearch',
'search_button.x'=37,
'search_button.y'=12
);
$fields_string = http_build_query ( $fields_post, '' );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "URL?reqCode=gotoSearchqueryKind=2postcode=".$zip);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt($ch, CURLOPT_REFERER, $refer );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login );
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar );
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string );
$data = https://www.04ip.com/post/curl_exec($ch);
preg_match_all('/id="table1"[s]*?tr[s]*?td class="maintext"[sS]*?/td[s]*?/tr/', $data, $matches);
if (!$handle = fopen($filename, 'a ')) {
echo "不能打开文件 $filename";
echo "n";
exit;
}
if (fwrite($handle, $matches[0][1]) === FALSE) {
echo "不能写入到文件 $filename";
echo "n";
exit;
}
echo "成功地将 $somecontent 写入到文件$filename";
echo "n";
fclose($handle);
curl_close($ch);
}
class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function get_microtime()
{
list($usec,$sec)=explode(' ',microtime());return((float)$usec (float)$sec);
}
function start()
{
$this-StartTime=$this-get_microtime();
}
function stop(){
$this-StopTime=$this-get_microtime();
}
function spent()
{
return ($this-StopTime-$this-StartTime);
}
}
$runtime-stop ();
$con = 'Processed in'.$runtime-spent().'seconds';
echo 'Processed in'. $runtime-spent().'seconds';
模拟POST请求 提交数据或上传文件 .
.
代码如下 复制代码
发送POST请求
function execUpload(){
$file = '/doucment/Readme.txt';
$ch = curl_init();
$post_data = https://www.04ip.com/post/array(
'loginfield' = 'username',
'username' = 'ybb',
'password' = '123456',
'file' = '@d:usrwwwtranslatedocumentReadme.txt'
);
curl_setopt($ch, CURLOPT_HEADER, false);
//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样 。
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_URL, '');
$info= curl_exec($ch);
curl_close($ch);
print_r($info);
}
2.
function handleUpload(){
print_r($_POST);
echo '===file upload info:';
print_r($_FILES);
}
■cURL 函数
■curl_close — 关闭一个cURL会话
■curl_copy_handle — 复制一个cURL句柄和它的所有选项
■curl_errno — 返回最后一次的错误号
■curl_error — 返回一个保护当前会话最近一次错误的字符串
■curl_exec — 执行一个cURL会话
■curl_getinfo — 获取一个cURL连接资源句柄的信息
■curl_init — 初始化一个cURL会话
■curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄
■curl_multi_close — 关闭一组cURL句柄
■curl_multi_exec — 运行当前 cURL 句柄的子连接
■curl_multi_getcontent — 如果设置了CURLOPT_RETURNTRANSFER , 则返回获取的输出的文本流
■curl_multi_info_read — 获取当前解析的cURL的相关传输信息
■curl_multi_init — 返回一个新cURL批处理句柄
■curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源
■curl_multi_select — 等待所有cURL批处理中的活动连接
■curl_setopt_array — 为cURL传输会话批量设置选项
■curl_setopt — 设置一个cURL传输选项
■curl_version — 获取cURL版本信息
怎么用PHP发送POST请求PHP发送POST请求的三种方式
class Request{
public static function post($url, $post_datahttps://www.04ip.com/post/= '', $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 = https://www.04ip.com/post/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;
}
}
【php发送post数据 php发送post请求api】php发送post数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php发送post请求api、php发送post数据的信息别忘了在本站进行查找喔 。

    推荐阅读