十个超级有用的PHP代码片断
php中文网
发布时间:2016-06-13 10:30:09
|
1102人浏览过
|
来源于php中文网
原创
十个超级有用的PHP代码片段
1. 发送短信
调用 TextMagic
API。
02
|
require
(
'textmagic-sms-api-php/TextMagicAPI.php'
);
|
05
|
$username
=
'myusername'
;
|
06
|
$password
=
'mypassword'
;
|
09
|
$router
=
new
TextMagicAPI(
array
(
|
10
|
????
'username'
=>
$username
,
|
11
|
????
'password'
=>
$password
|
15
|
$result
=
$router
->send(
'Wake up!'
,
array
(9991234567), true);
|
2. 根据IP查找地址
01
|
function
detect_city(
$ip
) {
|
03
|
????????
$default
=
'UNKNOWN'
;
|
05
|
????????
if
(!
is_string
(
$ip
) ||
strlen
(
$ip
)
$ip
==
'127.0.0.1'
||
$ip
==
'localhost'
)
|
06
|
????????????
$ip
=
'8.8.8.8'
;
|
08
|
????????
$curlopt_useragent
=
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'
;
|
10
|
????????
$url
=
'http://ipinfodb.com/ip_locator.php?ip='
. urlencode(
$ip
);
|
11
|
????????
$ch
= curl_init();
|
13
|
????????
$curl_opt
=
array
(
|
14
|
????????????
CURLOPT_FOLLOWLOCATION? => 1,
|
15
|
????????????
CURLOPT_HEADER????? => 0,
|
16
|
????????????
CURLOPT_RETURNTRANSFER? => 1,
|
17
|
????????????
CURLOPT_USERAGENT?? =>
$curlopt_useragent
,
|
18
|
????????????
CURLOPT_URL?????? =>
$url
,
|
19
|
????????????
CURLOPT_TIMEOUT???????? => 1,
|
20
|
????????????
CURLOPT_REFERER???????? =>
'http://'
.
$_SERVER
[
'HTTP_HOST'
],
|
23
|
????????
curl_setopt_array(
$ch
,
$curl_opt
);
|
25
|
????????
$content
= curl_exec(
$ch
);
|
27
|
????????
if
(!
is_null
(
$curl_info
)) {
|
28
|
????????????
$curl_info
= curl_getinfo(
$ch
);
|
31
|
????????
curl_close(
$ch
);
|
33
|
????????
if
( preg_match(
'{City : ([^}i'
,
$content
,
$regs
) )? {
|
34
|
????????????
$city
=
$regs
[1];
|
36
|
????????
if
( preg_match(
'{State/Province : ([^}i'
,
$content
,
$regs
) )? {
|
37
|
????????????
$state
=
$regs
[1];
|
40
|
????????
if
(
$city
!=
''
&&
$state
!=
''
){
|
41
|
??????????
$location
=
$city
.
', '
.
$state
;
|
42
|
??????????
return
$location
;
|
44
|
??????????
return
$default
;
|
3. 显示网页的源代码
2
|
$lines
= file(
'http://google.com/'
);
|
3
|
foreach
(
$lines
as
$line_num
=>
$line
) {
|
5
|
????
echo
"Line #{$line_num} : "
. htmlspecialchars(
$line
) .
" \n"
;
|
4. 检查服务器是否使用HTTPS
立即学习“PHP免费学习笔记(深入)”;
jQuery和CSS3超酷3D堆叠式幻灯片特效源码
本特效源码是一款基于jQuery和CSS3插件hubSlider实现的超酷3D堆叠式幻灯片的代码。该幻灯片将各个slide堆叠在一起,利用jquery,CSS3 transforms和transitions来制作上下切换的堆叠幻灯片效果。本代码适用浏览器:搜狗、360、FireFox、Chrome、Safari、Opera、傲游、世界之窗,不支持IE8及以下浏览器。有兴趣的朋友们可以来下载试试吧
下载
1
|
if
(
$_SERVER
[
'HTTPS'
] !=
"on"
) {
|
2
|
????
echo
"This is not HTTPS"
;
|
4
|
????
echo
"This is HTTPS"
;
|
5. 显示Facebook fans数量
1
|
function
fb_fan_count(
$facebook_name
){
|
3
|
????
$data
= json_decode(
file_get_contents
(
"https://graph.facebook.com/"
.
$facebook_name
));
|
6. 检测图片的主要颜色
01
|
$i
= imagecreatefromjpeg(
"image.jpg"
);
|
03
|
for
(
$x
=0;
$x
$i
);
$x
++) {
|
04
|
????
for
(
$y
=0;
$y
$i
);
$y
++) {
|
05
|
????????
$rgb
= imagecolorat(
$i
,
$x
,
$y
);
|
06
|
????????
$r ?? = (
$rgb
>> 16) & 0xFF;
|
07
|
????????
$g ?? = (
$rgb
>>? & 0xFF;
|
08
|
????????
$b ?? =
$rgb
& 0xFF;
|
17
|
$rAverage
=
round
(
$rTotal
/
$total
);
|
18
|
$gAverage
=
round
(
$gTotal
/
$total
);
|
19
|
$bAverage
=
round
(
$bTotal
/
$total
);
|
7. 获取内存使用信息
01
|
echo
"Initial: "
.memory_get_usage().
" bytes \n"
;
|
08
|
????
$array
[]= md5(
$i
);
|
13
|
????
unset(
$array
[
$i
]);
|
16
|
echo
"Final: "
.memory_get_usage().
" bytes \n"
;
|
21
|
echo
"Peak: "
.memory_get_peak_usage().
" bytes \n"
;
|
8. 使用 gzcompress() 压缩数据
02
|
"Lorem ipsum dolor sit amet, consectetur
|
03
|
adipiscing elit. Nunc ut elit id mi ultricies
|
04
|
adipiscing. Nulla facilisi. Praesent pulvinar,
|
05
|
sapien vel feugiat vestibulum, nulla dui pretium orci,
|
06
|
non ultricies elit lacus quis ante. Lorem ipsum dolor
|
07
|
sit amet, consectetur adipiscing elit. Aliquam
|
08
|
pretium ullamcorper urna quis iaculis. Etiam ac massa
|
09
|
sed turpis tempor luctus. Curabitur sed nibh eu elit
|
10
|
mollis congue. Praesent ipsum diam, consectetur vitae
|
11
|
ornare a, aliquam a nunc. In id magna pellentesque
|
12
|
tellus posuere adipiscing. Sed non mi metus, at lacinia
|
13
|
augue. Sed magna nisi, ornare in mollis in, mollis
|
14
|
sed nunc. Etiam at justo in leo congue mollis.
|
PHP速学教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
下载
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn