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

2022-02-09 04:02:59 字數 2517 閱讀 5045

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

1

<?php

2$url='';

3$html = file_get_contents($url

);

4echo

$html

; 5 ?>

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

1

<?php

2$fp = fopen($url, 'r'); 3//

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

1

stream_get_meta_data($fp);

1

while(!feof($fp

))

4echo "url body: $result";

5fclose($fp

);

6 ?>

7

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

1

<?php

2$data = array ('foo' => 'bar');

1

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

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

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

1

<?php

2function get_url ($url,$cookie=false

) 3

else

20fclose($fp

);

21return

$result

; 22

} 23

} 24

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

25function geturlhtml($url,$cookie=false

) 26

34return

false

; 35

} 36 ?>

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

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

1

<?php

2$ch =curl_init();

3$timeout = 5;

4 curl_setopt ($ch, curlopt_url, '');

5 curl_setopt ($ch, curlopt_returntransfer, 1);

6 curl_setopt ($ch, curlopt_connecttimeout, $timeout

);

7$file_contents = curl_exec($ch

);

8 curl_close($ch

);

9echo

$file_contents

; 10 ?>

**:

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...