十个超级有用的php代码片段
1. 发送短信
调用 textmagic
api。
01
// include the textmagic php lib
02
require
(
'textmagic-sms-api-php/textmagicapi.php'
);
03
?
04
// set the username and password information
05
$username
=
'myusername'
;
06
$password
=
'mypassword'
;
07
?
08
// create a new instance of tm
09
$router
=
new
textmagicapi(
array
(
10
'username'
=>
$username
,
11
'password'
=>
$password
12
));
13
?
14
// send a text message to '999-123-4567'
15
$result
=
$router
->send(
'wake up!'
,
array
(9991234567), true);
16
?
17
//
result:? result is: array ( [messages] => array ( [19896128] =>
9991234567 ) [sent_text] => wake up! [parts_count] => 1 )
2. 根据ip查找地址
01
function
detect_city(
$ip
) {
02
?
03
$default
=
'unknown'
;
04
?
05
if
(!
is_string
(
$ip
) ||
strlen
(
$ip
) < 1 ||
$ip
==
'127.0.0.1'
||
$ip
==
'localhost'
)
06
$ip
=
'8.8.8.8'
;
07
?
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)'
;
09
?
10
$url
=
'http://ipinfodb.com/ip_locator.php?ip='
. urlencode(
$ip
);
11
$ch
= curl_init();
12
?
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'
],
21
);
22
?
23
curl_setopt_array(
$ch
,
$curl_opt
);
24
?
25
$content
= curl_exec(
$ch
);
26
?
27
if
(!
is_null
(
$curl_info
)) {
28
$curl_info
= curl_getinfo(
$ch
);
29
}
30
?
31
curl_close(
$ch
);
32
?
33
if
( preg_match(
'{<li>city : ([^<]*)</li>}i'
,
$content
,
$regs
) )? {
34
$city
=
$regs
[1];
35
}
36
if
( preg_match(
'{<li>state/province : ([^<]*)</li>}i'
,
$content
,
$regs
) )? {
37
$state
=
$regs
[1];
38
}
39
?
40
if
(
$city
!=
''
&&
$state
!=
''
){
41
$location
=
$city
.
', '
.
$state
;
42
return
$location
;
43
}
else
{
44
return
$default
;
45
}
46
?
47
}
3. 显示网页的源代码
1
<?php
// display source code
2
$lines
= file(
'http://google.com/'
);
3
foreach
(
$lines
as
$line_num
=>
$line
) {
4
// loop thru each line and prepend line numbers
5
echo
line #<b>{$line_num}</b> :
. htmlspecialchars(
$line
) .
<br>\n
;
6
}
4. 检查服务器是否使用https
1
if
(
$_server
[
'https'
] !=
on
) {
2
echo
this is not https
;
3
}
else
{
4
echo
this is https
;
5
}
5. 显示facebook fans数量
1
function
fb_fan_count(
$facebook_name
){
2
// example: https://graph.facebook.com/digimantra
3
$data
= json_decode(
file_get_contents
(
https://graph.facebook.com/
.
$facebook_name
));
4
echo
$data
->likes;
5
}
6. 检测图片的主要颜色
01
$i
= imagecreatefromjpeg(
image.jpg
);
02
?
03
for
(
$x
=0;
$x
<imagesx(
$i
);
$x
++) {
04
for
(
$y
=0;
$y
<imagesy(
$i
);
$y
++) {
05
$rgb
= imagecolorat(
$i
,
$x
,
$y
);
06
$r
= (
$rgb
>> 16) & 0xff;
07
$g
= (
$rgb
>>? & 0xff;
08
$b
=
$rgb
& 0xff;
09
?
10
$rtotal
+=
$r
;
11
$gtotal
+=
$g
;
12
$btotal
+=
$b
;
13
$total
++;
14
}
15
}
16
?
17
$raverage
=
round
(
$rtotal
/
$total
);
18
$gaverage
=
round
(
$gtotal
/
$total
);
19
$baverage
=
round
(
$btotal
/
$total
);
7. 获取内存使用信息
01
echo
initial:
.memory_get_usage().
bytes \n
;
02
/* prints
03
initial: 361400 bytes
04
*/
05
?
06
// let's use up some memory
07
for
(
$i
= 0;
$i
< 100000;
$i
++) {
08
$array
[]= md5(
$i
);
09
}
10
?
11
// let's remove half of the array
12
for
(
$i
= 0;
$i
< 100000;
$i
++) {
13
unset(
$array
[
$i
]);
14
}
15
?
16
echo
final:
.memory_get_usage().
bytes \n
;
17
/* prints
18
final: 885912 bytes
19
*/
20
?
21
echo
peak:
.memory_get_peak_usage().
bytes \n
;
22
/* prints
23
peak: 13687072 bytes
24
*/
8. 使用 gzcompress() 压缩数据
01
$string
=
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.
15
&l