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)境,還要看它的請求是否被壓縮和轉(zhuǎn)碼還有就是
//在框架中或者說php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函數(shù)來轉(zhuǎn)化文件了
//注意你要發(fā)送的服務(wù)器的header頭的結(jié)構(gòu)和特殊參數(shù),實(shí)在不行就自己構(gòu)建一個。廢話不多說,直接上代碼。
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>阿會計師的賀卡收到框架</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選項詳細(xì)說明可參考https://www.jb51.net/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ù)操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
上一篇:PHP上傳圖片到數(shù)據(jù)庫并顯示的實(shí)例代碼
欄 目:PHP編程
下一篇:thinkphp5實(shí)現(xiàn)微信掃碼支付
本文標(biāo)題:PHP CURL實(shí)現(xiàn)模擬登陸并上傳文件操作示例
本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/11004.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
 - 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
 - 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
 - 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
 - 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
 - 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
 - 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
 - 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
 - 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
 - 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文件的語句
 


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
 - 04-02關(guān)于txt數(shù)據(jù)庫php的信息
 - 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
 - 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
 - 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
 - 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
 - 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
 - 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
 - 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
 - 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 01-10delphi制作wav文件的方法
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 


