php模擬http請求

2021-07-16 12:06:48 字數 2652 閱讀 4849

http請求有get,post。

php傳送http請求有三種方式[我所知道的有三種,有其他的告訴我]。

file_get_contents();詳情見:

curl傳送請求。

fsocket傳送。

下面說使用curl傳送。

首先環境需要配置好curl元件。

1

2

3

4

5

6

7

8

9

10

在windows中讓php支援curl比較簡單:

在php.ini中將extension=php_curl.dll前面的分號去掉,

有人說需要將php根目錄的libeay32.dll和ssleay32.dll需要拷貝到系統目錄下去。我實驗不拷貝也可以。

在linux中,如果使用原始碼安裝,需要在make 之前,./configure --with-curl=path,

其中,path是你的 libcurl庫的位置,比如你安裝libcurl庫之後,

path可能就是/usr/local/,libcurl可以是靜態庫,也可以是動態庫。

注意libcurl庫configure的時候,可以將一些不需要的功能去掉,

比如ssl , ldap等。在php configure的時候,會去檢查libcurl中某些功能是否被開啟,

進而去相應地調整生成的php

兩個使用curl發請求的例子。

//

初始化乙個 curl 物件

$curl =curl_init();

//設定你需要抓取的url

curl_setopt($curl, curlopt_url, '');

//設定header 響應頭是否輸出

curl_setopt($curl, curlopt_header, 1);

//設定curl 引數,要求結果儲存到字串中還是輸出到螢幕上。

// 1如果成功只將結果返回,不自動輸出任何內容。如果失敗返回false

curl_setopt($curl, curlopt_returntransfer, 0);

//執行curl,請求網頁

$data = curl_exec($curl

); //

關閉url請求

curl_close($curl

); //

顯示獲得的資料

print_r($data

);

再乙個post方式的例子:

1

//post方式

設定是通過post還是get方法

10 curl_setopt($ch,curlopt_post,1);

11//

傳遞的變數

12 curl_setopt($ch,curlopt_postfields,$curlpost

); 13

$data = curl_exec($ch

);14 curl_close($ch);

在這個http://mytest/lab/t.php檔案中:
1

if(!empty($_post

))

就先寫這麼多。

3echo "以下是get方式的響應內容:

";4 sock_get($gurl);5

function sock_get($url) 6

17} 18

1920

//fsocket模擬post提交

22echo "以下是post方式的響應內容:

";23 sock_post($purl,"uu=rrrrrrrrrrrr&&kk=mmmmmm");

24function sock_post($url, $query

) 25

42 }

PHP模擬http請求

方法一 利用php的socket程式設計來直接給介面傳送資料來模擬post的操作。建立兩個檔案post.php,getpost.php post.php內容如下 flag 0 params errno errstr 要post的資料 argv array var1 abc var2 how are ...

php 模擬http請求

原文 guzzlehttp模擬表單提交 並用nodejs接受資料 return response js接收資料 const bodyparser require body parser server.listen 80 use bodyparser.urlencoded use bodyparser...

PHP(10)PHP模擬HTTP請求

php可以通過模擬http協議發起http請求 curl是乙個非常強大的開源庫,支援很多協議,包括http ftp telnet等,我們使用它來傳送http請求。它給我們帶來的好處是可以通過靈活的選項設定不同的http協議引數,並且支援https。curl可以根據url字首是 http 還是 htt...