您好,登錄后才能下訂單哦!
怎么在PHP中利用CURL模擬登陸并上傳文件?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
<?php header('content-type:text/html;charset=gb2312'); //要注意你需要上傳的網站服務器的運行環境,還要看它的請求是否被壓縮和轉碼還有就是 //在框架中或者說php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函數來轉化文件了 //注意你要發送的服務器的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);//執行結果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//參數設置,是否顯示頭部信息,1為顯示,0為不顯示 curl_setopt($ch, CURLOPT_REFERER, $http_url); //表單數據,是正規的表單設置值為非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)); //執行并獲取結果 $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" , "發布"); $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);
關于怎么在PHP中利用CURL模擬登陸并上傳文件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。