PHP 系列 (二)

获取微信头像时, 将头像地址尾部的 /0 替换成 /96, 即是获取微信头像缩略图

1
2
3
4
5
<?php
$headImg = 'http://wx.qlogo.cn/mmopen/3NKFAMZl61HUWEYUkOK5oPib1GqOlMWYk7tt1N6WNeqvychibiaU4A0nynlYz4aOHcvNdhDla0bicpDJOdZJrQ6u6vANnktos0eI/0';
if (($index = strrpos($headImg, '/0')) !== false) {
$thumbHeadImg = substr_replace($headImg, '/96', $index);
}

伪造IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 伪造IP
*
* @param string $url
*
* @return mixed
*/
function forged_ip($url)
{
$header = array(
'CLIENT-IP:' . '111.111.111.' . rand(1, 254),
'X-FORWARDED-FOR:' . '111.111.111.' . rand(1, 254),
);
$userAgentArr = array(
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar)',
'Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; Alexa Toolbar)',
'Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; SV1)',
$_SERVER['HTTP_USER_AGENT']
);
$userAgent = $userAgentArr[rand(0, 3)];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7);
$result = curl_exec($ch);
curl_close($ch);

return $result;
}

使用 ReflectionClass ReflectionProperty 创建实例并初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
class User
{
private $id;

private $nickname;

private $age;

private $registerTime;
}

// 用户数据
$userData = [
'id' => 1,
'nickname' => 'test',
'age' => 12,
'registerTime' => new DateTime(),
];
// 类名
$className = 'User';
// 反射类
$reflectionClass = new ReflectionClass($className);
// 创建类的实例
$instance = $reflectionClass->newInstanceWithoutConstructor();
foreach ($userData as $key => $val) {
// 类属性已定义
if ($reflectionClass->hasProperty($key)) {
// 获取类属性的相关信息
$reflectionProperty = new ReflectionProperty($className, $key);
// 属性未公开
if (!$reflectionProperty->isPublic()) {
// 将属性设置为可访问
$reflectionProperty->setAccessible(true);
}

// 设置属性值
$reflectionProperty->setValue($instance, $val);
}
}
var_dump($instance);

使用 ReflectionClass ReflectionMethod 实现前置/后置方法功能模块, 执行带参数的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class IndexController
{
public function indexAction(){
echo 'index<br />';
}

public function testAction($year = 2012, $month = 2, $day = 21)
{
echo $year . '--------' . $month . '-----------' . $day . '<br />';
}

public function _before_index()
{
echo __FUNCTION__ . '<br />';
}

public function _after_index()
{
echo __FUNCTION__ . '<br />';
}
}

// 执行index方法
$method = new ReflectionMethod('IndexController', 'indexAction');
// 进行权限判断
if ($method->isPublic()) {
$class = new ReflectionClass('IndexController');
// 执行前置方法
if ($class->hasMethod('_before_index')) {
$beforeMethod = $class->getMethod('_before_index');
if ($beforeMethod->isPublic()) {
$beforeMethod->invoke(new IndexController);
}
}

$method->invoke(new IndexController);

// 执行后置方法
if($class->hasMethod('_after_index')){
$beforeMethod = $class->getMethod('_after_index');
if ($beforeMethod->isPublic()) {
$beforeMethod->invoke(new IndexController);
}
}
}

// 执行带参数的方法
$method = new ReflectionMethod('IndexController', 'testAction');
$params = $method->getParameters();
foreach ($params as $param ) {
$paramName = $param->getName();
if (isset($_REQUEST[$paramName])) {
$args[] = $_REQUEST[$paramName];
} elseif ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
}
}
if (count($args) == $method->getNumberOfParameters()) {
$method->invokeArgs(new IndexController, $args);
} else {
echo 'parameters is not match!';
}

ArrayAccess 使对象可以像数组一样被访问
JsonSerializable 自定义JSON化的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class Configuration implements JsonSerializable, ArrayAccess
{
private static $config;
private $configArray;

private function __construct()
{
$this->configArray = [
'a' => 1,
'b' => 2
];
}

public static function instance()
{
if (self::$config == null) {
self::$config = new Configuration();
}

return self::$config;
}

public function jsonSerialize()
{
return 'Custom Json:' . serialize(new Configuration());
}

public function offsetExists($offset)
{
return isset($this->configArray[$offset]);
}

public function offsetGet($offset)
{
return $this->configArray[$offset];
}

public function offsetSet($offset, $value)
{
$this->configArray[$offset] = $value;
}

public function offsetUnset($offset)
{
unset($this->configArray[$offset]);
}
}

$configuration = Configuration::instance();

var_dump($configuration['a']); // 输出 1
$configuration['a'] = 3;
var_dump($configuration['a']); // 输出 3

var_dump(isset($configuration['b'])); // 输出 true
var_dump(isset($configuration['c'])); // 输出 false

unset($configuration['b']);
var_dump(isset($configuration['b'])); // 输出 false

var_dump(json_encode($configuration)); // 输出 Custom Json\uff1aO:13:\"Configuration\":1:{s:26:\"\u0000Configuration\u0000configArray\";a:2:{s:1:\"a\";i:1;s:1:\"b\";i:2;}}
0%