php 常用功能

function curl_get_page($url){

    //初始化

    $ch = curl_init();

   //设置选项,包括url

     curl_setopt($ch, CURLOPT_URL, $url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
     curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
     curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
     curl_setopt($ch,CURLOPT_AUTOREFERER,1);
     curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取html内容

     $output = curl_exec($ch);

   //释放curl句柄

    curl_close($ch);

    return $output ;
}
private function curl_post_page( $url , $data , $timeout = 4 ) {
$ch = curl_init (); curl_setopt ( $ch , CURLOPT_URL , $url );
curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST , "POST" );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , FALSE );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , FALSE );
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt ( $ch , CURLOPT_AUTOREFERER , 1 );
 
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
$output = curl_exec ( $ch );
if
( curl_errno ( $ch )){
echo curl_error ( $ch );
}
return $output ;
}


//
自己包装的 post 方法
private function
httpPost 接口 ( $url , $post = null ){
$context = array ();
if (! is_array ( $post )) return false ;
ksort ( $post );
$context [ 'http' ] = array ( 'timeout' => 60 , 'method' => 'POST' , 'content' => http_build_query ( $post , '' , '&' ));
return file_get_contents ( $url , false , stream_context_create ( $context ) ); }
//
自己包装的 get 方法 private function httpGet 接口 ( $url ){
 
$opts = array ( 'http' => array ( 'method' => "GET" , 'timeout' => 15 ,));
 
$context = @ stream_context_create ( $opts ); $
result
= @ file_get_contents ( $url , false , $context );
 
return $result ; }
/**************************************************************

*
* 自己包装 将数组转换为 JSON 字符串(兼容中文)
* @param array $array 要转换的数组
*
@return string 转换得到的 json 字符串
*
@access public *
*************************************************************/

public function
fJSON 返回 ( $array ) {
$this ->arrayRecursive( $array , 'urlencode' , true );
$json = json_encode( $array );
return urldecode ( $json ); }
// 自己包装 将返回数据拼装成数组
private function
f 组装返回数组 ( $Result , $ErrorStr , $ResultData ){
$return = array (
"Result" => $Result ,
"ErrorStr" => $ErrorStr ,
"ResultData"
=> $ResultData
);

return
$return ;
}

/**************************************************************
*
*
自己拼装 使用特定 function 对数组中所有元素做处理
*
@param string &$array 要处理的字符串
* @param string $function 要执行的函数
*
@return boolean $apply_to_keys_also 是否也应用到 key
*
@access public *
*************************************************************/
private function arrayRecursive(& $array , $function , $apply_to_keys_also = false ) {
static
$recursive_counter = 0 ;
if
(++ $recursive_counter > 1000 )
{

die
( 'possible deep recursion attack' );
}
foreach ( $array as $key => $value ) {
  if ( is_array ( $value )) {
$this ->arrayRecursive( $array [ $key ], $function , $apply_to_keys_also );
}
else {
$array [ $key ] = $function ( $value );
}

if ( $apply_to_keys_also && is_string ( $key )) {
$new_key = $function ( $key );
 
if ( $new_key != $key ) {
$array [ $new_key ] = $array [ $key ];
  unset ( $array [ $key ]);
}

}

}

$recursive_counter --;
}

public function fs 随机字符串 ( $length ){
$str = null ;
$strPol
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz" ;
$max = strlen ( $strPol )- 1 ;
for ( $i = 0 ; $i < $length ; $i ++){
$str .= $strPol [ rand ( 0 , $max )]; //rand($min,$max) 生成介于 min max 两个数之间的一个随机整数
}
return
$str ;
}

你可能感兴趣的:(函数)