PHP CURL實現模擬登陸并上傳文件操作示例
更新時間:2020年01月02日 09:18:54 作者:luyaran
這篇文章主要介紹了PHP CURL實現模擬登陸并上傳文件操作,結合實例形式分析了PHP使用curl進行模擬登陸與文件傳輸操作具體實現技巧,需要的朋友可以參考下
本文實例講述了PHP CURL實現模擬登陸并上傳文件操作。分享給大家供大家參考,具體如下:
<?php header('content-type:text/html;charset=gb2312'); //要注意你需要上傳的網站服務器的運行環(huán)境,還要看它的請求是否被壓縮和轉碼還有就是 //在框架中或者說php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函數來轉化文件了 //注意你要發(fā)送的服務器的header頭的結構和特殊參數,實在不行就自己構建一個。廢話不多說,直接上代碼。 function curl_form($post_data,$sumbit_url,$http_url,$cookie_file){ $headers = array(); $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; $headers[] = 'Cache-Control: max-age=0'; $headers[] = 'Accept-Encoding: gzip, deflate'; $headers[] = 'Origin: http://my.***.com'; $headers[] = 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'; $headers[] = 'Upgrade-Insecure-Requests: 1'; $headers[] = 'Content-Type: application/x-www-form-urlencoded'; $headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0'; $headers[] = 'Connection: keep-alive'; // $headers[] = 'Cookie: ASPSESSIONIDCCTCTQQC=KBGLPDKBIKDIDCBGFOKNMKOE'; //初始化 $ch = curl_init(); //設置變量 curl_setopt($ch, CURLOPT_URL, $sumbit_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//執(zhí)行結果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//參數設置,是否顯示頭部信息,1為顯示,0為不顯示 curl_setopt($ch, CURLOPT_REFERER, $http_url); //表單數據,是正規(guī)的表單設置值為非0 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888'); // curl_setopt($ch, CURLOPT_ENCODING, ""); // curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); //執(zhí)行并獲取結果 $output = curl_exec($ch); if($output === FALSE) { echo "<br/>","cUrl Error:".curl_error($ch); }else{ return $output; } // 釋放cURL句柄 curl_close($ch); } $temp = array(); $temp['title'] = iconv( "utf-8", "gb2312//IGNORE" , "牛排店加盟"); $temp['ly'] = iconv( "utf-8", "gb2312//IGNORE" , "特色餐飲加盟"); $temp['classid'] = iconv( "utf-8", "gb2312//IGNORE" , "7159"); $temp['newssort'] = iconv( "utf-8", "gb2312//IGNORE" , "1"); $temp['panduan'] = iconv( "utf-8", "gb2312//IGNORE" , "0"); $temp['submit_button'] = iconv( "utf-8", "gb2312//IGNORE" , "發(fā)布"); $temp['addr'] = iconv( "utf-8", "gb2312//IGNORE" , "bjcanyin"); $temp['ContentBg'] = ""; $temp['newss'] = iconv( "utf-8", "gb2312//IGNORE" , htmlspecialchars_decode("<p>阿會計師的賀卡收到框架</p><p><img alt=\"\" src=\" http://localhost/super/Uploads/img/2017-08-10/598c145a9527e.jpg\" style=\"height:243px; width:324px\" /></p>", ENT_QUOTES)); $cookie_file = dirname(__FILE__)."/jdzj.tmp"; $sumbit_url = "http://***/news/***.asp"; $http_url="http://***/news/***.asp?act=addok"; $img = curl_form($temp,$http_url,$sumbit_url,$cookie_file); var_dump($img);
PS:關于PHP curl選項詳細說明可參考http://www.dbjr.com.cn/article/39331.htm
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php curl用法總結》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP數據結構與算法教程》及《PHP中json格式數據操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細解答2013-01-01PHP設計模式之模板方法模式Template Method Pattern詳解
在我們實際開發(fā)中,如果一個方法極其復雜時,如果我們將所有的邏輯寫在一個方法中,那維護起來就很困難,要替換某些步驟時都要重新寫,這樣代碼的擴展性就很差,當遇到這種情況就要考慮今天的主角——模板方法模式2022-12-12