PHP 中的CURL 模擬表單的post提交

2021-09-26 20:18:41 字數 2968 閱讀 6366

廢話不多說啦,直接上**:

<?php

$data= ['username'=>'喬峰','skill'=>'擒龍手'];

$headers=array();

$curl= curl_init();// 啟動乙個curl會話

curl_setopt($curl, curlopt_url,$url);// 要訪問的位址

curl_setopt($curl, curlopt_ssl_verifypeer, 0);// 對認證證書**的檢查

curl_setopt($curl, curlopt_ssl_verifyhost, 0);// 從證書中檢查ssl加密演算法是否存在

curl_setopt($curl, curlopt_useragent,$_server['http_user_agent']);// 模擬使用者使用的瀏覽器

curl_setopt($curl, curlopt_followlocation, 1);// 使用自動跳轉

curl_setopt($curl, curlopt_autoreferer, 1);// 自動設定referer

curl_setopt($curl, curlopt_post, 1);// 傳送乙個常規的post請求

curl_setopt($curl, curlopt_postfields, http_build_query($data)); // post提交的資料報

curl_setopt($curl, curlopt_timeout, 30);// 設定超時限制防止死迴圈

curl_setopt($curl, curlopt_header, 0);// 顯示返回的header區域內容

curl_setopt($curl, curlopt_returntransfer, 1);// 獲取的資訊以檔案流的形式返回

$result= curl_exec($curl);// 執行操作

if(curl_errno($curl))

curl_close($curl);// 關閉curl會話

echo($result);

?>

這裡需要注意的是:

要想以 x-www-form-urlencoded 方式傳送,最關鍵是傳送的資料格式。

方式from-data試傳送的資料用的是array格式,而方式為 x-www-form-urlencoded 時需要用key=value&key2=value2的格式傳送,傳送的是string型的資料。

比如我上面from-data資料的為:

​​​​​​$data = [

'username' => '喬峰',

'skill' => '擒龍手'

];x-www-form-urlencoded時的資料則要變為

無論從事什麼行業,只要做好兩件事就夠了,乙個是你的專業、乙個是你的人品,專業決定了你的存在,人品決定了你的人脈,剩下的就是堅持,用善良專業和真誠贏取更多的信任。

總結:這個特重要:curl_setopt($curl, curlopt_postfields, http_build_query($data)); // post提交的資料報

php中實現curl模擬Http請求

前幾天做了 簡訊和 郵件,其中 簡訊用到了curl來模擬傳送http請求,那麼今天就詳細的說一下如何用curl來傳送http請求。curl是利用url的語法規則來傳輸檔案 資料的工具 二 curl應用場景 需求1 有兩個php檔案 a.php和b.php a.php需要向b.php檔案中提交一些資料...

PHP中curl的使用

說明 5.4 curl上傳檔案只支援 語法 5.5 支援 語法和curlfile類 大於 5.6 只支援curlfile類 相容性寫法參考示例 function curl upload url,source else data array file realpath source 5.5 curl ...

php中curl模擬post提交多維陣列

今天需要用curl模擬post提交引數,請求同事提供的乙個介面 但是傳遞的引數中,有乙個引數的值為陣列,用普通的curl post 提交,會報錯誤 php notice array to string conversion in test functions.php on line 30 notic...