curl 模擬POST方法 傳送資料

2021-08-07 17:12:24 字數 1248 閱讀 8485

用post方法傳送資料

當發起get請求時,資料可以通過「查詢字串」(query string)傳遞給乙個url。例如,在google中搜尋時,搜尋關鍵即為url的查詢字串的一部分:

這種情況下你可能並不需要curl來模擬。把這個url丟給「file_get_contents()」就能得到相同結果。 

不過有一些html表單是用post方法提交的。這種表單提交時,資料是通過 http請求體(request body) 傳送,而不是查詢字串。例如,當使用codeigniter論壇的表單,無論你輸入什麼關鍵字,總是被post到如下頁面:

你可以用php指令碼來模擬這種url請求。首先,新建乙個可以接受並顯示post資料的檔案,我們給它命名為post_output.php:

print_r($_post);

接下來,寫一段php指令碼來執行curl請求:

以下為引用的內容:

時候將獲取資料返回

// 我們在post資料哦!

curl_setopt($ch, curlopt_post, 1); //設定為post傳輸

// 把post的變數加上

curl_setopt($ch, curlopt_postfields, $post_data); //post過去資料

$output = curl_exec($ch);

if($output === false)

$info = curl_getinfo($ch);  //能夠在curl執行後獲取這一請求的有關資訊

curl_close($ch);

echo $output;

執行**後應該會得到以下結果:

這段指令碼傳送乙個post請求給 post_output.php ,這個頁面 $_post 變數並返回,我們利用curl捕捉了這個輸出。

curl模擬傳送post請求

curl模擬傳送post請求 初始化 curl curl init 設定抓取的url curl setopt curl,curlopt url,設定標頭檔案的資訊作為資料流輸出 curl setopt curl,curlopt header,1 設定獲取的資訊以檔案流的形式返回,而不是直接輸出。cu...

curl模擬http傳送get或post介面測試

curl模擬http傳送get或post介面測試 可參照 一 get請求 curl i 顯示全部資訊 curl l 只顯示頭部資訊 curl v 顯示get請求全過程解析 wget 也可以 二 post請求 curl d param1 value1 m2 value2 三 json格式的post請求...

CURL模擬post請求

開發專案需要用curl模擬post提交乙個多維陣列資料,請求另外乙個專案的乙個介面 傳遞的引數中,有乙個引數的值為陣列,而且很可能是乙個很大的多維陣列。但是當我使用普通的curl post 提交,會報錯誤,錯誤提示如下 php notice array to string conversion 根據...