PHP curl 模擬http https請求

2021-07-30 03:37:22 字數 3357 閱讀 9341

/***

* 模擬使用者瀏覽器post

***/

public static function vpost($url, $data)

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

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

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

// curl_setopt($curl, curlopt_ssl_verifyhost, 1); // 從證書中檢查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, $data); // post提交的資料報

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

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

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

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

$ret = ['status' => 1];

//print_r(curl_getinfo($curl));

if ($error = curl_errno($curl)) else

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

return $ret; // 返回資料

}

/**https 請求

* @param $url

* @param $data

* @param array $header

* @return array

* author fox

*/if (is_array($data))

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

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

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

curl_setopt($curl, curlopt_ssl_verifyhost, 2); // 從證書中檢查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, $data); // post提交的資料報

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

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

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

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

$ret = ['status' => 1];

//print_r(curl_getinfo($curl));

if ($error = curl_errno($curl)) else

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

return $ret; // 返回資料

}

注意,如果請求目標位址後返回了502,可能是開發者的開發環境未安裝openssl,

brew install curl --with-openssl

參考:

public function curlpost($url = , $post = , $time_out = 20, $header = )

if ($header)

$result = curl_exec($curl);

$error = curl_error($curl);

curl_close($curl);

if ($error) else

}

如果是post raw json/xml,則呼叫時的**為:

$senddata    = ["name"=>"張三丰"];

使用curl post資料時,如果post的資料大於1024位元組,curl並不會直接就發起post請求。而是會分兩步(詳見http協議:

1、傳送乙個請求,header中包含乙個expect:100-continue,詢問server是否願意接受資料。

2、接受到server返回的100-continue回應後,才把資料post到server。

來回兩次應答,導致傳輸比較慢。

post請求時,加上:

curl_setopt($ch, curlopt_httpheader, array("expect:"));
忽略詢問,直接傳輸資料。

PHP curl 模擬使用者登入

fakelogin.php 模擬post提交 url 表單的action處理程式 unset post data post data username name 帳號 post data password pass 密碼 post data type 登入表單的其他域內容.下略 post data ...

PHP CURL模擬登入 獲取資料

使用了curl 模擬登入 測試了公司 記錄下來 模擬登入方法 function login post url,cookie,post 登入成功後獲取資料 function get content url,cookie tempnam 函式建立乙個具有唯一檔名的臨時檔案。cookie tempnam ...

PHP CURL獲取cookies模擬登入的方法

要提取google搜尋的部分資料,發現google對於軟體抓取它的資料遮蔽的厲害,以前偽造下 user agent 就可以抓資料,但是現在卻不行了。利用抓包資料發現,google 判斷了 cookies,當你沒有cookies的時候,直接返回 302 跳轉,而且是連續幾十個302跳轉,根本抓不了資料...