php代码基础
如何接入新浪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代码基础的更多相关文章
- Android逆向-java代码基础
作者:I春秋作家——HAI_ 0×00 前言 看这篇可以先看看之前的文章,进行一个了解.Android逆向-java代码基础(1)Android逆向-java代码基础(2) 之前看到有大佬用smali ...
- 003-Python3-基础语法-运行方式、代码基础要求、运算符[算数运算符、比较运算符、赋值运算符、位运算符、逻辑运算符、成员运算符、身份运算符]、运算符优先级
一.基础语法 参看地址:https://www.runoob.com/python3/python3-tutorial.html 1.1.运行方式 1.文件方式 编写一个hello.py文件, pri ...
- 180114 用装饰器实现在不改变函数调用者的代码基础上,实现在函数执行前后分别打印"before" 和 "after"
def bef_aft(func): #定义一个名为bef_aft的函数名 ,()里的是函数的参数,设置为func, 函数的参数分为实参和形参,有个参数传递的概念 ,下面有很多的解释 def PRin ...
- Kafka 生产者和消费者入门代码基础
这篇博文讲解Kafka 的生产者和消费者实例. 基础版本一 生产者 ProducerFastStart.java package com.xingyun.tutorial_1; import org. ...
- IO—代码—基础及其用例
字节流:文件.图片.歌曲 使用字节流的应用场景:如果是读写的数据都不需要转换成字符的时候,则使用字节流. 字节流处理单元为1个字节, 操作字节和字节数组.不能直接处理Unicode字符 字节流可用于任 ...
- 面试代码基础(一)从strstr说起
对于写程序要注意:要能在面试官的提示下把代码写出来(把思想实现的能力)!还要注意边界检查!递归找到出口! 开场来个简单字符串匹配 int strstr(char* target,char* sourc ...
- java基础之JDBC二:原生代码基础应用
JDBC的基础应用CURD: 增删改 public void noQuery() { Connection conn = null; Statement stat = null; try { //注册 ...
- 【仿真】Carla介绍与基本使用 [1] (附代码 基础版)
0. 参考与前言 主要介绍无人驾驶的仿真环境CARLA,开源社区维护,以下为相关参考链接: Carla官方文档 建议后续找的时候 先按好版本号,有些功能/api 是新版本里有的 Carla官方gith ...
- django框架代码基础
urls.py 导入相对应的模块from django.conf.urls import url,includefrom django.contrib import adminfrom son1.vi ...
随机推荐
- C#使用Jquery zTree实现树状结构显示_异步数据加载
JQuery-Ztree下载地址:https://github.com/zTree/zTree_v3 JQuery-Ztree数结构演示页面: http://www.treejs.cn/v3/dem ...
- C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍
在前面一系列文章中,我们可以看到微信自定义菜单的重要性,可以说微信公众号账号中,菜单是用户的第一印象,我们要规划好这些菜单的内容,布局等信息.根据微信菜单的定义,我们可以看到,一般菜单主要分为两种,一 ...
- Chart: Who pays the most in Seattle for software engineers
http://www.geekwire.com/2012/chart-pays-seattle-software-engineers/ Chart: Who pays the most in Seat ...
- Swing布局管理器介绍
创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhangjunhd.blog.51cto.com/113473/128174 当选 ...
- php中实现的一个curl批处理的实例
curl是利用URL语法在命令行方式下工作的开源文件传输工具 本文在php中实现了的一个curl批处理的实例. 代码如下: header("Content-Type:text/html;ch ...
- C/C++ Memory Layout
参考 http://www.cnblogs.com/skynet/archive/2011/03/07/1975479.html
- 【译】Core Java Questions and Answers【1-33】
前言 译文链接:http://www.journaldev.com/2366/core-java-interview-questions-and-answers Java 8有哪些重要的特性 Java ...
- ABP集合贴
thead>tr>th,.table>tbody>tr>th,.table>tfoot>tr>th,.table>thead>tr>t ...
- 无脑简单 命令升级git Centos
yum remove git yum install zlib (系统默认已经装上) yum install zlib-devel ># wget https://github.com/git/ ...
- SpringMVC的执行流程(二)
文字解析: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配 DispatcherServlet的请求映射路径(在web.xml中指定),web容器将请求转交 ...