本文目錄一覽:
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方式發送數據
:用PHP向伺服器發送HTTP的POST請求,代碼如下:?php/***發送post請求*@paramstring$url請求地址*@paramarray$post_datapost鍵值對數據*@returnstring*/.
怎麼用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;
}
}
如何用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請求並獲取網頁?
$post=’POST數據’;
// 初始化
$curl = curl_init(‘URL’);
$header = array();
$header[] = ‘User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36’;
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// 不輸出header頭信息
curl_setopt($curl, CURLOPT_HEADER, 0);
// 保存到字元串而不是輸出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// post數據
curl_setopt($curl, CURLOPT_POST, 1);
// 請求數據
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
// 是否抓取跳轉後的頁面
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/279464.html