1、post https提交 方法
function curl_post2($url='', $postdata='', $options=array()){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_TIMEOUT, 20); if (!empty($options)){ curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; }
2、普通post提交
//通過curl模擬post的請求; function SendDataByCurl($url,$data=array()){ //對空格進行轉義 $url = str_replace(' ','+',$url); $ch = curl_init(); //設置選項,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定義超時3秒鍾 // POST數據 curl_setopt($ch, CURLOPT_POST, 1); // 把post的變量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //所需傳的數組用http_bulid_query()函數處理一下,就ok了 //執行並獲取url地址的內容 $output = curl_exec($ch); $errorCode = curl_errno($ch); //釋放curl句柄 curl_close($ch); if(0 !== $errorCode) { return false; } return $output; } function curl_post($url='', $postdata='', $options=array()){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (!empty($options)){ curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; }
3、curl get提交
function curl_get($url='', $options=array()){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (!empty($options)){ curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; }
參考:
function vpost($url,$data,$cookie){ // 模擬提交數據函數 $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_COOKIE, $cookie); curl_setopt($curl, CURLOPT_REFERER,'https://www.baidu.com');// 設置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_HEADER, 0); // 顯示返回的Header區域內容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 執行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓異常 } curl_close($curl); // 關閉CURL會話 return $tmpInfo; // 返回數據 }