函数效果:
/** * 正则匹配img标签src * @param $tag * @return array */ function extract_attrib($tag) { preg_match_all('/(id|alt|title|src)=("[^"]*")/i', $tag, $matches); $ret = array(); foreach($matches[1] as $i => $v) { $ret[$v] = $matches[2][$i]; } return $ret; }
function gf_dump($var) { echo '<pre>'; print_r($var); echo '</pre>'; }
function gf_now($time = 0) { $time = empty($time) ? time() : $time; return date('Y-m-d H:i:s', $time); }
/** * 安全的获取客户端IP * @return string */ function gf_get_remote_addr() { if(!empty($_SERVER["HTTP_CLIENT_IP"])) { $chp = $_SERVER["HTTP_CLIENT_IP"]; } else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { $chp = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if(!empty($_SERVER["REMOTE_ADDR"])) { $chp = $_SERVER["REMOTE_ADDR"]; } else { $chp = ''; } // ipv4 & ipv6 preg_match("/[d.]{7,15}|[:0-9a-fA-F]+/", $chp, $chps); $chp = isset($chps[0]) ? $chps[0] : 'unknown'; unset($chps); return $chp; }
/** * http post 请求 * @param [string] $url [description] * @param [string] $parameters [description] * @param array $headers [description] * @return [obj] [description] */ function gf_http_post($url,$parameters = NULL, $headers = array()){ return gf_http($url,'post' ,$parameters , $headers ); } /** * http get 请求 * @param [string] $url [description] * @param [string] $parameters [description] * @param array $headers [description] * @return [obj] [description] */ function gf_http_get($url, $parameters = NULL,$headers = array()){ return gf_http($url,'get' ,$parameters , $headers ); } /** * [gf_http description] * @param [type] $url [description] * @param [type] $method [description] * @param [type] $parameters [description] * @param array $headers [description] * @return [type] [description] */ function gf_http($url, $method, $parameters = NULL, $headers = array()) { if(empty($url)){return NULL;} $ch = curl_init(); /* Curl settings */ curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0); curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE); curl_setopt($ch, CURLOPT_TIMEOUT , 3000); curl_setopt($ch, CURLOPT_HEADER , FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , 2); // 从证书中检查SSL加密算法是否存在 switch (strtolower($method)) { case 'post': curl_setopt($ch, CURLOPT_POST, TRUE); if (!empty($parameters)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); } break; case 'get': if (!empty($parameters)) { $url .= strpos($url, '?') === false ? '?' : '&'; $url .= http_build_query($parameters); } break; default: # code... break; } curl_setopt($ch, CURLOPT_URL , $url ); curl_setopt($ch, CURLOPT_HTTPHEADER , $headers); curl_setopt($ch, CURLINFO_HEADER_OUT , TRUE ); $response = curl_exec($ch); curl_close ($ch); return $response; }