thinkphp5模拟post请求_Thinkphp5.1模拟登录并提交form表单
最近項目需要,要遠程登錄別人的系統,并且在對方的系統中提交表單,所以寫了一個類。
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\facade\Env;
use think\facade\Request;
class Offline extends Controller
{
//域名
public $baseUrl = 'http://localhost:8080';
//登錄錯誤返回的鏈接
public $loginErrorUrl = '/login';
//登錄驗證
public $loginUrl = '/check';
//登錄成功跳轉
public $loginSuccessUrl = '/loginSuccess';
//提交表單
public $orderUrl = '/save';
//提交表單返回頁面URL
public $formUrl = '/proposal';
//下載URL
public $downloadUrl? ? ? ? ? ? ?= '/downloads';
//記錄登錄返回的cookie
public $cookieFile;
//日志
public $logFile;
protected function initialize()
{
parent::initialize();
$this->cookieFile? ? ? ? ? ? = Env::get('root_path') .'runtime/success_cookie.txt';
$this->logFile? ? ? ? ? ? ? ?= Env::get('root_path') .'runtime/offline_log.txt';
}
/**
* @function? ? online
* @intro? ? ? ? 模擬form提交表單
* @return? string
*/
public function online()
{
$ch ? ? ? ? ? ? ? ? ? = curl_init($this->baseUrl.$this->formUrl);
curl_setopt($ch,CURLOPT_COOKIEFILE, $this->cookieFile); //同時發送Cookie
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$headers ? ? ? ? ? ? ? ? ? ?= curl_getinfo($ch);
curl_close($ch);
//通過返回的header里的url來判斷登錄狀態
if(strpos($this->loginErrorUrl, $headers['url'])){
return 'login_fail';
}
//跟進傳過來的ID去查詢訂單內容
$orderId ? ? ? ? ? ? ? ? ? ? ? ? ? ? = input('id');
//訂單信息
$order? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = db('order')->where(['id'=>$orderId])->find();
$form ? ? = [];
$form['name'] ? ? ? ? ? ? ? ? ? ? ? ? ? ? = $order['title'];
$form['type'] ? ? ? ? ? ? ? ? ? ? ? ? ? ? = 1;
//提交表單
$headersandcontent? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = $this->curlPostForm($this->baseUrl.$this->orderUrl, $this->cookieFile, $form);
$headers = $headersandcontent['header'];
$content = strip_tags($headersandcontent['content']);//過濾html標簽
$content = preg_replace("/(.*?)/si","",$content);//過濾script標簽
$content = preg_replace("/\s+/", " ", $content);//過濾回車
$content = str_replace('?', '', $content);//過濾空格
$this->log(json_encode($headers));
$this->log($content);
//對返回的內容進行判斷
if(strpos($headers['url'], $this->orderUrl)){
return 'success';
}else{
return 'fail';
}
}
//下載PDF
public function downloadpdf($no){
$url? ? = $this->baseUrl.$this->downloadUrl.'?no='.$no;
$save_path? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = Env::get('root_path').'public/uploads/pdf/';
$filename? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?= $no.'.pdf';
$content? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? = $this->curlDownloadFile($url,$this->cookieFile);
// 保存文件到制定路徑
$length = file_put_contents($save_path.$filename, $content);
if($length == 0){
unlink($save_path.$filename);
if(!empty($content)){
$fp2 = @fopen($save_path . $filename, 'a');
fwrite($fp2, $content);
fclose($fp2);
}
}
return true;
}
//模擬登錄
public function login(){
$arr['username']? = '11111';
$arr['password']? = 'aaaa';
//模擬登錄,抓取返回值里的URL
$headers? ? ? ? ? ? = $this->curlPost($this->baseUrl.$this->loginUrl, $this->cookieFile, $arr);
//登錄成功
if(strpos($headers['url'], $this->loginSuccessUrl)){
return 'success';
}else{
return 'fail';
}
}
//記錄日志
public function log($str){
$fp = fopen($this->logFile, 'a+');
fwrite($fp, date("Y-m-d H:i:s").' '.$str." \r\n");
fclose($fp);
}
/**
* 模擬登錄
* @param string $url 提交到的地址
* @param string $cookie 保存cookie的文件
* @param string $arr 提交時要post的參數
* @return string $headers 返回的內容
*/
public function curlPost($url, $cookie, $arr=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!empty($arr)){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr)); //提交查詢信息
}
//若給定url自動跳轉到新的url,有了下面參數可自動獲取新url內容:302跳轉
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//設置cURL允許執行的最長秒數。
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //把返回來的cookie信息保存在$cookie文件中
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$content = curl_exec($ch);
//獲取請求返回碼,請求成功返回200
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//獲取一個cURL連接資源句柄的信息。
//$headers 中包含跳轉的url路徑
$headers = curl_getinfo($ch);
curl_close($ch);
return $headers;
}
/**
* 提交表單
* @param string $url 提交到的地址
* @param string $cookie 保存cookie的文件
* @param string $arr 提交時要post的參數
* @return string $headers 返回的內容
*/
public function curlPostForm($url, $cookie, $arr=''){
$headers = array('Content-Type: application/x-www-form-urlencoded');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //同時發送Cookie
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($arr)); //提交查詢信息
//若給定url自動跳轉到新的url,有了下面參數可自動獲取新url內容:302跳轉
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//設置cURL允許執行的最長秒數。
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$headers? ? = curl_getinfo($ch);
$content? ? = curl_exec($ch);
curl_close($ch);
return ['header'=>$headers,'content'=>$content];
}
/**
* 下載
* @param string $url 地址
* @param string $cookie 保存cookie的文件
* @return string $content 返回的內容
*/
public function curlDownloadFile($url, $cookie){
$headers = array('Content-Type: application/x-www-form-urlencoded');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //同時發送Cookie
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//設置cURL允許執行的最長秒數。
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$content? ? = curl_exec($ch);
curl_close($ch);
return $content;
}
}
這里的返回值基本都是html內容所以我這邊是判斷的返回值里的URL是不是匹配,可以根據需要調整。
總結
以上是生活随笔為你收集整理的thinkphp5模拟post请求_Thinkphp5.1模拟登录并提交form表单的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魏鹏机器人_痴迷农业科技,他发明智能分拣
- 下一篇: 动态规划算法php,php算法学习之动态