联系我们:416326801#qq.com(#改成@)

Latest from the blog

PHP函数header设置http状态码

标签: PHP http

//设置HTTP状态吗
public static function HTTP($num) {
    $http = array(
        100 => "HTTP/1.1 100 Continue",
        101 => "HTTP/1.1 101 Switching Protocols",
        200 => "HTTP/1.1 200 OK",
        201 => "HTTP/1.1 201 Created",
        202 => "HTTP/1.1 202 Accepted",
        203 => "HTTP/1.1 203 Non-Authoritative Information",
        204 => "HTTP/1.1 204 No Content",
        205 => "HTTP/1.1 205 Reset Content",
        206 => "HTTP/1.1 206 Partial Content",
        300 => "HTTP/1.1 300 Multiple Choices",
        301 => "HTTP/1.1 301 Moved Permanently",
        302 => "HTTP/1.1 302 Found",
        303 => "HTTP/1.1 303 See Other",
        304 => "HTTP/1.1 304 Not Modified",
        305 => "HTTP/1.1 305 Use Proxy",
        307 => "HTTP/1.1 307 Temporary Redirect",
        400 => "HTTP/1.1 400 Bad Request",
        401 => "HTTP/1.1 401 Unauthorized",
        402 => "HTTP/1.1 402 Payment Required",
        403 => "HTTP/1.1 403 Forbidden",
        404 => "HTTP/1.1 404 Not Found",
        405 => "HTTP/1.1 405 Method Not Allowed",
        406 => "HTTP/1.1 406 Not Acceptable",
        407 => "HTTP/1.1 407 Proxy Authentication Required",
        408 => "HTTP/1.1 408 Request Time-out",
        409 => "HTTP/1.1 409 Conflict",
        410 => "HTTP/1.1 410 Gone",
        411 => "HTTP/1.1 411 Length Required",
        412 => "HTTP/1.1 412 Precondition Failed",
        413 => "HTTP/1.1 413 Request Entity Too Large",
        414 => "HTTP/1.1 414 Request-URI Too Large",
        415 => "HTTP/1.1 415 Unsupported Media Type",
        416 => "HTTP/1.1 416 Requested range not satisfiable",
        417 => "HTTP/1.1 417 Expectation Failed",
        500 => "HTTP/1.1 500 Internal Server Error",
        501 => "HTTP/1.1 501 Not Implemented",
        502 => "HTTP/1.1 502 Bad Gateway",
        503 => "HTTP/1.1 503 Service Unavailable",
        504 => "HTTP/1.1 504 Gateway Time-out"
    );
    header($http[$num]);
}

PHP利用fsockopen异步处理数据

标签: PHP

PHP发送一个http请求,不等待结构返回,继续执行...

不会因为远程服务器慢导致页面超时!本函数不同于file_get_content:访问url不耗费时间,但没有返回值。

//异步处理数据
public static function doRequest($host, $path = '/', $param = array()) {
    $query = isset($param) ? http_build_query($param) : '';
    $port = 80;
    $errno = 0;
    $errstr = '';
    $timeout = 10;
    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

    if( $fp ) {
        !$path && $path = '/';
        $out = "POST ".$path." HTTP/1.1\r\n";
        $out .= "host:".$host."\r\n";
        $out .= "content-length:".strlen($query)."\r\n";
        $out .= "content-type:application/x-www-form-urlencoded\r\n";
        $out .= "connection:close\r\n\r\n";
        $out .= $query;
        fputs($fp, $out);
        fclose($fp);
   }
}


PHP发送POST请求

标签: PHP

//请求并返回内容
public static function curlPostData($url, $data = array(), $timeout = 5) {
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'MZAdmin');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8')); //构造IP 
    //curl_setopt($ch, CURLOPT_REFERER, "http://jishu8.net"); //构造来路 
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //POST数据
    if( $data ) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //判断是否为json
    if( is_string($data) && is_array(json_decode($data, true)) ) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: '.strlen($data)
                )
       );
    }
    $response = curl_exec($ch);
    if( $response === FALSE ) {
        echo "cURL Error: ".curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    if( $httpCode == 200 ) {
        return $response;
    } else {
        $err = array('errcode' => $httpCode, 'errmsg' => 'cURL Error', 'errdesc' => $httpInfo);
        return json_encode($err, JSON_UNESCAPED_UNICODE);
    }
}

PHP校验18位身份证号码

标签: PHP

//校验身份证号码
public static function checkCardId($crad_id) { 
	if(strlen($crad_id) != 18) { 
		return false; 
	} 
	$crad_id_base = substr($crad_id, 0, 17); 
	if ( self::checkCardIdBase($crad_id_base) != strtoupper(substr($crad_id, 17, 1)) ){ 
		return false; 
	} else {
		return true;	
	}
} 


// 计算身份证校验码,根据国家标准GB 11643-1999 
private static function checkCardIdBase($crad_id_base) { 
	if(strlen($crad_id_base) != 17) { 
		return false; 
	} 
	//加权因子 
	$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
	//校验码对应值 
	$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
	$checksum = 0; 
	for ($i = 0; $i < strlen($crad_id_base); $i++) 
	{ 
		$checksum += substr($crad_id_base, $i, 1) * $factor[$i]; 
	} 

	$mod = $checksum % 11; 
	$verify_number = $verify_number_list[$mod]; 
	return $verify_number; 
} 
1 2 3 4 5 6 7 8 ... Next »