i_array_column和_curl 和 json2array自定义函数

来源:互联网 发布:阿里云 安全组规则 编辑:程序博客网 时间:2024/05/19 00:36
/** * tp中实现array_column的功能 */function i_array_column($input, $columnKey, $indexKey=null){    if(!function_exists('array_column') || is_array($columnKey)){        $columnKeyIsNumber  = (is_numeric($columnKey)) ? true : false;        $indexKeyIsNull     = (is_null($indexKey))     ? true : false;        $indexKeyIsNumber   = (is_numeric($indexKey))  ? true : false;        $result = array();        foreach((array)$input as $key=>$row){            if($columnKeyIsNumber){  //如果是数字索引                $tmp= array_slice($row, $columnKey, 1);                $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null;            }else{  //判断是多列还是单列还是全部列                //是数组且长度大于1                if( is_array( $columnKey ) ){                    if( count( $columnKey ) > 1 ){                       foreach ($columnKey as $tmpKey) {                            $tmp[$tmpKey] = $row[$tmpKey];                        }                    } else {  //是数组但长度不大于1的时候直接将其取出                        $columnKey = array_shift( $columnKey );                    }                }                if ( is_string( $columnKey ) ){                    $tmp= isset($row[$columnKey])?$row[$columnKey]:null;                } else if ( is_null( $columnKey ) ){                    $tmp = $row;                }            }            if(!$indexKeyIsNull){                if($indexKeyIsNumber){                    $key = array_slice($row, $indexKey, 1);                    $key = (is_array($key) && !empty($key))?current($key):null;                    $key = is_null($key)?0:$key;                }else{                    $key = isset($row[$indexKey])?$row[$indexKey]:0;                }            }            $result[$key] = $tmp;        }        return $result;    }else{        return array_column($input, $columnKey, $indexKey);    }}
/** * 模拟请求,兼容post和get */function _curl($URL = '', $post_data = array(),$timeout=0){    $cu = curl_init();    if($post_data){        curl_setopt($cu, CURLOPT_POST, 1);//post提交方式        curl_setopt($cu, CURLOPT_POSTFIELDS, $post_data);//post提交的数据    }    curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE);    curl_setopt($cu, CURLOPT_URL, $URL);    curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//在发起连接前等待的时间,如果设置为0,则无限等待。    $response = curl_exec($cu);    curl_close($cu);    return $response;}


/**     * 模拟请求,兼容get,post,put,delete     * @param string $url     * @param string $param     * @param string $header     * @return mixed     */    public function _curl($url,$method='get',$param='', $header='')    {         $method=strtoupper($method);        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);                 //抓取指定网页        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);           // 增加 HTTP Header(头)里的字段        curl_setopt ( $ch, CURLOPT_HEADER, true );        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);        if($method == 'POST'){            curl_setopt($ch, CURLOPT_POST, 1);                      //post请求            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);        }        if($method =='PUT'){            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //put请求            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);        }                if($method =='DELETE'){            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //delete请求        }        $data = curl_exec($ch);        curl_close($ch);        return $data;            }


//获取json参数    protected function json2array()    {        $input = file_get_contents('php://input');//         file_put_contents('log.html', $input);        $input = urldecode($input);        $input = substr($input, strpos($input, '{'));        file_put_contents('log.html', $input);        $data = json_decode($input, true);                return $data;    }



0 0
原创粉丝点击