PHP CURL實(shí)現(xiàn)模擬登陸并上傳文件操作示例
本文實(shí)例講述了PHP CURL實(shí)現(xiàn)模擬登陸并上傳文件操作。分享給大家供大家參考,具體如下:
<?php header('content-type:text/html;charset=gb2312'); //要注意你需要上傳的網(wǎng)站服務(wù)器的運(yùn)行環(huán)境,還要看它的請(qǐng)求是否被壓縮和轉(zhuǎn)碼還有就是 //在框架中或者說php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函數(shù)來轉(zhuǎn)化文件了 //注意你要發(fā)送的服務(wù)器的header頭的結(jié)構(gòu)和特殊參數(shù),實(shí)在不行就自己構(gòu)建一個(gè)。廢話不多說,直接上代碼。 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(); //設(shè)置變量 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í)行結(jié)果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//參數(shù)設(shè)置,是否顯示頭部信息,1為顯示,0為不顯示 curl_setopt($ch, CURLOPT_REFERER, $http_url); //表單數(shù)據(jù),是正規(guī)的表單設(shè)置值為非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í)行并獲取結(jié)果 $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>阿會(huì)計(jì)師的賀卡收到框架</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:關(guān)于PHP curl選項(xiàng)詳細(xì)說明可參考http://chabaoo.cn/article/39331.htm
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP CURL獲取cookies模擬登錄的方法
- PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法
- PHP中使用CURL模擬登錄并獲取數(shù)據(jù)實(shí)例
- php下使用curl模擬用戶登陸的代碼
- php使用curl模擬登錄后采集頁面的例子
- PHP Curl模擬登錄微信公眾平臺(tái)、新浪微博實(shí)例代碼
- php curl 模擬登錄并獲取數(shù)據(jù)實(shí)例詳解
- PHP使用Curl實(shí)現(xiàn)模擬登錄及抓取數(shù)據(jù)功能示例
- php curl 上傳文件代碼實(shí)例
- PHP實(shí)現(xiàn)通過CURL上傳文件功能示例
- php使用curl模擬瀏覽器表單上傳文件或者圖片的方法
相關(guān)文章
PHP中header和session_start前不能有輸出原因分析
在http傳輸文本中,規(guī)定必須 header和content順序必須是:header在前content在后,并且header的格式必須滿足“keyword: value\n”這種格式,大家知道這是為什么嗎?接下來為您詳細(xì)解答2013-01-01PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法
下面小編就為大家分享一篇PHP 使用openssl 擴(kuò)展實(shí)現(xiàn)公鑰加密的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03PHP設(shè)計(jì)模式之模板方法模式Template Method Pattern詳解
在我們實(shí)際開發(fā)中,如果一個(gè)方法極其復(fù)雜時(shí),如果我們將所有的邏輯寫在一個(gè)方法中,那維護(hù)起來就很困難,要替換某些步驟時(shí)都要重新寫,這樣代碼的擴(kuò)展性就很差,當(dāng)遇到這種情況就要考慮今天的主角——模板方法模式2022-12-12