Yii框架靈活的擴展受到公司的青睞,所以,專案中使用了yii,為了相容於原來的系統,依然選擇了yii1.1的版本。
這裡不講yii的特性,主要說一說使用yii時對curl的再次封裝。
先看看yii的設定文件,並在main.php中將curl配置為Components。
'components' => array( // Curl库 调用:Yii::app()->curl 'curl' => array( 'class' => 'ext.curl.Curl', 'options' => array( CURLOPT_HTTPHEADER => array('Expect:'), CURLOPT_HEADER => '', ), ),
<?php /** * Curl wrapper for Yii * @author hackerone */ class Curl extends CComponent { private $_ch; private $response; // config from config.php public $options; // default config private $_config = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HEADER => false, CURLOPT_VERBOSE => true, CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_TIMEOUT => 30, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' ); private function exec($url) { $this->setOption(CURLOPT_URL, $url); $this->response = curl_exec($this->_ch); if (!curl_errno($this->_ch)) { if (isset($this->options[CURLOPT_HEADER])) if ($this->options[CURLOPT_HEADER]) { $header_size = curl_getinfo($this->_ch, CURLINFO_HEADER_SIZE); return substr($this->response, $header_size); } return $this->response; } else { throw new CException(curl_error($this->_ch)); return false; } } public function get($url, $params = array()) { $this->setOption(CURLOPT_HTTPGET, true); return $this->exec($this->buildUrl($url, $params)); } /** * weimob 内部接口post请求专用 * * 支持多维数组 和 文件上传 * */ public function httpPostRequest($url,$data = array()){ $this->setOption(CURLOPT_POST, true); $this->setOption(CURLOPT_POSTFIELDS, http_build_query($data)); return $this->exec($url); } /** * Curl 原生post请求发送方式 * * 用于需要post无key参数、post文件等 */ public function post($url, $data = array()) { $this->setOption(CURLOPT_POST, true); $this->setOption(CURLOPT_POSTFIELDS, $data); return $this->exec($url); } public function put($url, $data, $params = array()) { // write to memory/temp $f = fopen('php://temp', 'rw+'); fwrite($f, $data); rewind($f); $this->setOption(CURLOPT_PUT, true); $this->setOption(CURLOPT_INFILE, $f); $this->setOption(CURLOPT_INFILESIZE, strlen($data)); return $this->exec($this->buildUrl($url, $params)); } public function delete($url, $params = array()) { $this->setOption(CURLOPT_RETURNTRANSFER, true); $this->setOption(CURLOPT_CUSTOMREQUEST, 'DELETE'); return $this->exec($this->buildUrl($url, $params)); } public function buildUrl($url, $data = array()) { $parsed = parse_url($url); isset($parsed['query']) ? parse_str($parsed['query'], $parsed['query']) : $parsed['query'] = array(); $params = isset($parsed['query']) ? array_merge($parsed['query'], $data) : $data; $parsed['query'] = ($params) ? '?' . http_build_query($params) : ''; if (!isset($parsed['path'])) { $parsed['path']='/'; } $parsed['port'] = isset($parsed['port'])?':'.$parsed['port']:''; return $parsed['scheme'].'://'.$parsed['host'].$parsed['port'].$parsed['path'].$parsed['query']; } public function setOptions($options = array()) { curl_setopt_array($this->_ch, $options); return $this; } public function setOption($option, $value) { curl_setopt($this->_ch, $option, $value); return $this; } public function setHeaders($header = array()) { if ($this->isAssoc($header)) { $out = array(); foreach ($header as $k => $v) { $out[] = $k .': '.$v; } $header = $out; } $this->setOption(CURLOPT_HTTPHEADER, $header); return $this; } private function isAssoc($arr) { return array_keys($arr) !== range(0, count($arr) - 1); } public function getError() { return curl_error($this->_ch); } public function getInfo() { return curl_getinfo($this->_ch); } public function getStatus() { return curl_getinfo($this->_ch, CURLINFO_HTTP_CODE); } // initialize curl public function init() { try { $this->_ch = curl_init(); $options = is_array($this->options) ? ($this->options + $this->_config) : $this->_config; $this->setOptions($options); $ch = $this->_ch; // close curl on exit @Yii::app()->onEndRequest = function() use(&$ch){ curl_close($ch); }; } catch (Exception $e) { throw new CException('Curl not installed'); } } public function getHeaders() { $headers = array(); $header_text = substr($this->response, 0, strpos($this->response, "\r\n\r\n")); foreach (explode("\r\n", $header_text) as $i => $line) { if ($i === 0) { $headers['http_code'] = $line; } else { list ($key, $value) = explode(': ', $line); $headers[$key] = $value; } } return $headers; } }
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
以上就介紹了yii11中對CURL的再封裝,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!