分类目录归档:PHP

使用php://input


翻译一下手册:
If you want to receive application/json post data in your script you can not use $_POST. $_POST does only handle form data.
如果你希望接收application/json的post数据,你不能用$_POST,$_POST仅仅用来处理表单数据

Read from php://input instead. You can use fopen or file_get_contents.
用php://input替代读取数据,使用fopen或者file_get_contents方法

Example:
举例:

echo和return,echo相对于浏览器的!!!!!

1、从关键词意思上看:

echo 是输出字符串到浏览器

return主要是用于函数,返回一个值

2、echo 主要用于:php对浏览器的输出;

return主要用于处理函数的返回值

3、在大多数情况下,echo 和return的结果都是一样的。但如果出现函数嵌套或者函数比较多的情况,

echo会无效或者发生错误。所以处理函数的返回或者输出还是用return好。

4、比如我项目中碰到的这个问题,因为要在浏览器输出内容,所以用echo比较合适。如果是需要一个可以被其它函数调用的返回值的话,建议还是用return.

kds钩子添加查询条件

默认打印where条件如下:
[“where”] => array(1) {
[0] => array(2) {
[0] => string(3) “exp”
[1] => object(think\db\Expression)#38 (1) {
[“value”:protected] => string(8) “cid=’29′”
}
}
}

重组where条件:

public function yunqikdsIndexAjaxQueryBegin(&$params){

if($params[‘tablename’] == ‘creative_baoming’) {

$params[‘where’][] = Db::raw(“audit_status != 0”);

// dump($params[‘where’]);die();
}
}

这样不会覆盖上面的条件

end()和key() 获取数组最后一个元素值和键名(key)

end()和key() 获取数组最后一个元素值和键名(key)

先用end()将内部指针指向数组中的最后一个元素,再用key()函数返回数组内部指针当前指向元素的键名。

例子:

$tmparr = array('a','b','x',1,'z');
$tmparr2 = array('a','c');
echo end($tmparr); //返回4
echo "
";
var_dump($tmparr); //返回数组
echo "
";
echo key($tmparr); //返回4
echo "
";
echo key($tmparr2); //返回0

php文件上传基础知识

PHP

 

$_FILES 是一个预定义的数组,用来获取通过 POST 方法上传文件的相关信息。如果为单个文件上传,那么 $_FILES 为二维数组;如果为多个文件上传,那么 $_FILES 为三维数组。

建立一个 file.html 演示上传文件,其中的代码如下:

<html>

<head></head>

<body></body>

<form enctype=“multipart/form-data” action=“file.php” method=“POST”>

Send this file: <input name=“userfile” type=“file” />

<input type=“submit” value=“Send File” />

</form>

</html>

新建一个用于接收文件信息的 PHP 文件 file.php,代码如下:

<?php
echo “<pre>”;
print_r($_FILES);
?>

在 file.html 页面选择文件后,单击 Send File 按钮,将会在页面输出以下信息:

Array
(
    [userfile] => Array
    (
        [name] => Screen Shot 2016-05-12 at 18.13.24.png
        [type] => image/png
        [tmp_name] => /private/var/tmp/phplVHp3W
        [error] => 0
        [size] => 344925
    )
)

tp5分页,复制到/extend/page/page.php就能用

<?php
namespace page;

use think\Paginator;

class Page extends Paginator
{
    
    //首页
    protected function home()
    {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
        } else {
            return "<p>首页</p>";
        }
    }
    
    //上一页
    protected function prev()
    {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
        } else {
            return "<p>上一页</p>";
        }
    }
    
    //下一页
    protected function next()
    {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
        } else {
            return "<p>下一页</p>";
        }
    }
    
    //尾页
    protected function last()
    {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
        } else {
            return "<p>尾页</p>";
        }
    }
    
    //统计信息
    protected function info()
    {
        return "<p class='pageRemark'>共<b>" . $this->lastPage .
            "</b>页<b>" . $this->total . "</b>条数据</p>";
    }
    
    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        
        $block = [
            'first' => null,
            'slider' => null,
            'last' => null
        ];
        
        $side = 3;
        $window = $side * 2;
        
        if ($this->lastPage < $window + 6) {
            $block['first'] = $this->getUrlRange(1, $this->lastPage);
        } elseif ($this->currentPage <= $window) {
            $block['first'] = $this->getUrlRange(1, $window + 2);
            $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        } elseif ($this->currentPage > ($this->lastPage - $window)) {
            $block['first'] = $this->getUrlRange(1, 2);
            $block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
        } else {
            $block['first'] = $this->getUrlRange(1, 2);
            $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
            $block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
        }
        
        $html = '';
        
        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }
        
        if (is_array($block['slider'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['slider']);
        }
        
        if (is_array($block['last'])) {
            $html .= $this->getDots();
            $html .= $this->getUrlLinks($block['last']);
        }
        
        return $html;
    }
    
    /**
     * 渲染分页html
     * @return mixed
     */
    public function render()
    {
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '%s<div class="pagination">%s %s %s</div>',
                    $this->css(),
                    $this->prev(),
                    $this->getLinks(),
                    $this->next()
                );
            } else {
                return sprintf(
                    '%s<div class="pagination">%s %s %s %s %s %s</div>',
                    $this->css(),
                    $this->home(),
                    $this->prev(),
                    $this->getLinks(),
                    $this->next(),
                    $this->last(),
                    $this->info()
                );
            }
        }
    }
    
    /**
     * 生成一个可点击的按钮
     *
     * @param string $url
     * @param int $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<a href="' . htmlentities($url) . '" title="第"' . $page . '"页" >' . $page . '</a>';
    }
    
    /**
     * 生成一个禁用的按钮
     *
     * @param string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<p class="pageEllipsis">' . $text . '</p>';
    }
    
    /**
     * 生成一个激活的按钮
     *
     * @param string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<a href="" class="cur">' . $text . '</a>';
    }
    
    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots()
    {
        return $this->getDisabledTextWrapper('...');
    }
    
    /**
     * 批量生成页码按钮.
     *
     * @param array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls)
    {
        $html = '';
        
        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }
        
        return $html;
    }
    
    /**
     * 生成普通页码按钮
     *
     * @param string $url
     * @param int $page
     * @return string
     */
    protected function getPageLinkWrapper($url, $page)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }
        
        return $this->getAvailablePageWrapper($url, $page);
    }
    
    /**
     * 分页样式
     */
    protected function css()
    {
        return '  <style type="text/css">
            .pagination p{
                margin:0;
                cursor:pointer
            }
            .pagination{
                height:40px;
                padding:20px 0px;
            }
            .pagination a{
                display:block;
                float:left;
                margin-right:10px;
                padding:2px 12px;
                height:24px;
                border:1px #cccccc solid;
                background:#fff;
                text-decoration:none;
                color:#808080;
                font-size:12px;
                line-height:24px;
            }
            .pagination a:hover{
                color:#077ee3;
                background: white;
                border:1px #077ee3 solid;
            }
            .pagination a.cur{
                border:none;
                background:#077ee3;
                color:#fff;
            }
            .pagination p{
                float:left;
                padding:2px 12px;
                font-size:12px;
                height:24px;
                line-height:24px;
                color:#bbb;
                border:1px #ccc solid;
                background:#fcfcfc;
                margin-right:8px;

            }
            .pagination p.pageRemark{
                border-style:none;
                background:none;
                margin-right:0px;
                padding:4px 0px;
                color:#666;
            }
            .pagination p.pageRemark b{
                color:red;
            }
            .pagination p.pageEllipsis{
                border-style:none;
                background:none;
                padding:4px 0px;
                color:#808080;
            }
            .dates li {font-size: 14px;margin:20px 0}
            .dates li span{float:right}
        </style>';
    }
}

php的curl例子,摘自runoob.com

https://www.runoob.com/php/php-ref-curl.html

<?php
function geturl($url){
        $headerArray =array("Content-type:application/json;","Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output,true);
        return $output;
}


function posturl($url,$data){
        $data  = json_encode($data);    
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($outputtrue);
}


function puturl($url,$data){
    $data = json_encode($data);
    $ch = curl_init(); //初始化CURL句柄 
    curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output,true);
}

function delurl($url,$data){
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$put_url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");   
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
}

function patchurl($url,$data){
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH");  
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);     //20170611修改接口,用/id的方式传递,直接写在url中了
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output);
    return $output;
}
?>

php中的的isFile和is_file方法

SplFileInfo类
SplFileInfo::isFile($fullpathfilename)
参数必须是全路径

一下是image类的open方法返回的结果,tp5中的image类

object(think\Image)#33 (3) {
  ["im":protected] => resource(88) of type (gd)
  ["gif":protected] => NULL
  ["info":protected] => array(4) {
    ["width"] => int(4096)
    ["height"] => int(3072)
    ["type"] => string(4) "jpeg"
    ["mime"] => string(10) "image/jpeg"
  }
}