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教程有兴趣的朋友有所帮助。

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器