php傳送get post請求的幾種方法

2021-07-02 05:23:50 字數 3473 閱讀 1057

方法1: 用file_get_contents 以get方式獲取內容

[php]view plain

copy

print?

<?php  

$url

='';  $html

= file_get_contents

($url

);  

echo

$html

;  ?>  

方法2: 用fopen開啟url, 以get方式獲取內容

[php]view plain

copy

print?

<?php  

$fp= 

fopen

($url

, 'r'

);  

//返回請求流資訊(陣列:請求狀態,阻塞,返回值是否為空,返回值http頭等)

[php]view plain

copy

print?

stream_get_meta_data(

$fp);  

[php]view plain

copy

print?

while

(!feof

($fp

))   

echo

"url body: $result"

;  fclose($fp

);  

?>  

方法3:用file_get_contents函式,以post方式獲取url

[php]view plain

copy

print?

<?php  

$data

= array

('foo'

=> 

'bar'

);  

[php]view plain

copy

print?

//生成url-encode後的請求字串,將陣列轉換為字串

//生成請求的控制代碼檔案

方法4:用fsockopen函式開啟url,以get方式獲取完整的資料,包括header和body,fsockopen需要 php.ini 中 allow_url_fopen 選項開啟

[php]view plain

copy

print?

<?php  

function

get_url (

$url

,$cookie

=false)  

else

fclose($fp

);  

return

$result

;  }  

}  //獲取url的html部分,去掉header

function

geturlhtml(

$url

,$cookie

=false)  

return

false;  

}  ?>  

方法5:用fsockopen函式開啟url,以post方式獲取完整的資料,包括header和body

方法6:使用curl庫,使用curl庫之前,可能需要檢視一下php.ini是否已經開啟了curl擴充套件

[php]view plain

copy

print?

<?php  

$ch= curl_init();  

$timeout

= 5;  

curl_setopt ($ch

, curlopt_url, 

'');  

curl_setopt ($ch

, curlopt_returntransfer, 1);  

curl_setopt ($ch

, curlopt_connecttimeout, 

$timeout

);  

$file_contents

= curl_exec(

$ch);  

curl_close($ch

);  

echo

$file_contents

;  ?>  

PHP模擬傳送get post請求

模擬傳送post請求 模擬傳送get請求 curlopt header 啟用時會將標頭檔案的資訊作為資料流輸出。curlopt returntransfer true false 選項,也可以使用1 0代替 true 如果成功只將結果返回,不自動輸出任何內容 如果失敗返回false false 如果...

linux傳送get post請求

使用curl指令 普通請求 curl i i v 帶引數請求 curl v param1 1 m2 2 get請求攜帶的引數只到param1 1,符號在linux系統中為後台執行的操作符,此處需要使用反斜槓 轉義 使用wget指令 wget使用curl指令 curl d username user1...

php傳送get post請求的幾種方法

方法1 用file get contents 以get方式獲取內容 url html file get contents url echo html 方法2 用fopen開啟url,以get方式獲取內容 fp fopen url,r 返回請求流資訊 陣列 請求狀態,阻塞,返回值是否為空,返回值htt...