ThinkPHP学习日记(知识点总结)

A、ThinkPHP CURD操作时条件判断where的不同写法

      在早期查看ThinkPHP3.2的在线开发文档时发现所有的where()中判定的条件都是直接写入的,但是后期参与开发工作时发现where中的判定条件也可以使用数组array的形式来进行描述,array的使用对于多条件多数值的判断来说会大大的减少代码的编写量。

        a、where中直接写入判定条件

             $data = $User->where('status =1 AND name="thinkphp"' )->find();

        b、where中使用array写入判定条件

$post = $postview->where(array('postinfo_type' => array('in',array(0,2))))->order('id desc')->select();

B、ThinkPHP 的 C()函数

      在ThinkPHP的项目框架中的Conf目录下为配置目录,在开发过程中可先将事先定义好的array写入config.php,然后用C('XXXX')来直接调取数组。

         a、获取参数

       $userId = C('USER_ID') ;

               $userType = C('USER_TYPE') ;

         b、保存设置

               $config['user_id'] = 1 ;

               $config['user_type'] = 1 ;

               C($config,'name') ;

C、ThinkPHP中使用CURL请求API响应

       a、手动编写

                $ch = curl_init();
$post_data =array(
"access_token" => $tokenn,
"channel_id"   => $flag,
"price" => $price,
"category_id"   => $part,
"post_image"   => $photo,
"post_description"  => $des,
"map_type"          => 2,
"lat"               => 40.024572,
"lon"               => 116.300885
);
    $header = array();
    curl_setopt ( $ch , CURLOPT_URL, "http://121.43.xxx.xx/xxxxx/xxx/xxxxx" ) ; 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADER, $header);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   $result = curl_exec($ch);
   curl_close($ch);      

             b、但是手动编写实在是太麻烦了,个人感觉这些函数自己封装在一个类里,使用的时候直接实例化类然后传入参数即可,但是目前还没有自己封装过类,从度娘上拔一个下来先。

                    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class my_curl {
     
     public function __construct() {
         ! extension_loaded ( 'curl' ) && exit ( 'CURL扩展未加载,程序终止运行' );
         header ( 'Content-Type: text/html; charset=utf-8' );
     }
     
     public function go( $url , $vars = '' ) {
         $ch = curl_init ();
         $array [CURLOPT_URL] = $url ;
         $array [CURLOPT_HEADER] = false;
         $array [CURLOPT_RETURNTRANSFER] = true;
         $array [CURLOPT_FOLLOWLOCATION] = true;
         
         if (! empty ( $vars )) {
             $postfields = $this ->render_postfields ( $vars ); //生成urlcode的url
             $array [CURLOPT_POST] = true;
             $array [CURLOPT_POSTFIELDS] = $postfields ;
         }
         
         //判断是否有cookie,有的话直接使用
         if ( $_COOKIE [ 'cookie_jar' ] || is_file ( $_COOKIE [ 'cookie_jar' ] )) {
             $array [CURLOPT_COOKIEFILE] = $_COOKIE [ 'cookie_jar' ]; //这里判断cookie
         } else {
             $cookie_jar = tempnam ( '/tmp' , 'cookie' ); //产生一个cookie文件
             $array [CURLOPT_COOKIEJAR] = $cookie_jar ; //写入cookie信息
             setcookie ( 'cookie_jar' , $cookie_jar ); //保存cookie路径
         }
         curl_setopt_array ( $ch , $array ); //传入curl参数
         $content = curl_exec ( $ch ); //执行
         curl_close ( $ch ); //关闭
         return $content ; //返回
     }
     
     protected function render_postfields( $vars ) {
         foreach ( $vars as $key => $values ) {
             $postdata .= urlencode ( $key ) . "=" . urlencode ( $values ) . "&" ;
         }
         return $postdata ;
     }
}
?>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
if ( $_POST [ 'email' ]&& $_POST [ 'password' ]){
     include 'curl_class.php' ;
$curl = new my_curl ();
$vars [ 'email' ] = $_POST [ 'email' ];
$vars [ 'password' ] = $_POST [ 'password' ];
//$vars ['done'] = '/';
//$vars ['submit'] = '登 录';
echo $curl ->go ( 'http://www.renren.com/PLogin.do' , $vars );
 
} else {
?>
<html>
     <form action= "" class = "login-form" id= "loginForm" method= "post" >
         <input type= "text" id= "email" name= "email" />
         <input type= "text" id= "password" name= "password" />
         <input type= "submit" tabindex= "4" value= "登录" class = "input-submit" id= "login" >
     </form>
</html>
<?php }?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值