码迷,mamicode.com
首页 > Web开发 > 详细

php代码基础

时间:2016-04-10 17:49:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

  如何接入新浪api

<?php
function getWeiboData()
{
	$count = 15;
	// 参数source后面输入你的授权号
	$url = "https://api.weibo.com/2/statuses/home_timeline.json?source=123456789&count=".$count."&page=1";
	echo $url.‘<br />‘;
	
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	// 设置是否显示header信息 0是不显示,1是显示  默认为0
	//curl_setopt($curl, CURLOPT_HEADER, 0);
	// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。0显示在屏幕上,1不显示在屏幕上,默认为0
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	// 要验证的用户名密码
	curl_setopt($curl, CURLOPT_USERPWD, "username:password");
	$data = curl_exec($curl);
	curl_close($curl);
	
	$result = json_decode($data, true);
	
	echo ‘<pre>‘;
	print_r($result);
	echo ‘</pre>‘;
}
?>

  远程获取图片的大小

//用法 echo remote_filesize($url,$user=‘‘,$pw=‘‘);
$url = "http://www.nowamagic.net/librarys/images/random/rand_11.jpg";
echo remote_filesize($url,$user=‘‘,$pw=‘‘);

function remote_filesize($uri,$user=‘‘,$pw=‘‘)
{
// start output buffering
    ob_start();
// initialize curl with given uri
    $ch = curl_init($uri); // make sure we get the header
    curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request
    curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here
    if (!empty($user) && !empty($pw))
    {
        $headers = array(‘Authorization: Basic ‘ . base64_encode($user.‘:‘.$pw));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    $okay = curl_exec($ch);
    curl_close($ch); // get the output buffer
    $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings
    ob_end_clean();  // gets you the numeric value from the Content-Length // field in the http header
    $regex = ‘/Content-Length:\s([0-9].+?)\s/‘;
    $count = preg_match($regex, $head, $matches);  // if there was a Content-Length field, its value // will now be in $matches[1]
    if (isset($matches[1]))
    {
        $size = $matches[1];
    }
    else
    {
        $size = ‘unknown‘;
    }
    $last_mb = round($size/(1024*1024),3);
	$last_kb = round($size/1024,3);
    return $last_kb . ‘KB / ‘ . $last_mb.‘ MB‘;
}

  检查是否为手机登录并且跳转

$agent = check_wap();
if( $agent )
{
	header(‘Location: http://www.baidu.com‘);
	exit;
}

// //如果是手机的话
function check_wap(){
	// 先检查是否为wap代理,准确度高
	if(stristr($_SERVER[‘HTTP_VIA‘],"wap")){
		return true;
	}
	// 检查浏览器是否接受 WML.
	elseif(strpos(strtoupper($_SERVER[‘HTTP_ACCEPT‘]),"VND.WAP.WML") > 0){
		return true;
   }
   //检查USER_AGENT
   elseif(preg_match(‘/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i‘, $_SERVER[‘HTTP_USER_AGENT‘])){
		return true;		 	
	}
	else{
		return false;	
   }
}

  4函数urlencode对url为中文的时候进行转义编码

  

<?php  
//GB2312的Encode  
echo urlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.+  
echo urldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_.  
echo rawurlencode("中文-_. ")."\n"; //%D6%D0%CE%C4-_.%20  
echo rawurldecode("%D6%D0%CE%C4-_. ")."\n"; //中文-_.  
?>  

  

php代码基础

标签:

原文地址:http://www.cnblogs.com/kengdiexienima/p/5374477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!