关于php

php DOMDocument可以操作xml

 1 <?php
 2 
 3 //定义参数
 4 $data = array('sParam1' => 'test1', 'sParam2' => 101, 'isAuto' => 1);
 5 
 6 //把参数转换成URL数据
 7 $data = @http_build_query($data);
 8 
 9 $aContext = array(
10     'http' => array(
11         'method'  => 'GET',
12 
13         'header'  => 'Content-type: application/x-www-form-urlencoded',
14 
15         'content' => $data,
16     ),
17 );
18 
19 $cxContext = stream_context_create($aContext);
20 
21 //此处必须为完整路径
22 $sUrl = 'http://www.mytest.com/test.php';
23 
24 $d = @file_get_contents($sUrl, false, $cxContext);
25 
26 print_r($d);
27 
28 ?>

 

 1 <?php
 2 
 3 $data = array('sParam1' => 'test1', 'sParam2' => 101, 'isAuto' => 1); //定义参数
 4 
 5 $data = @http_build_query($data); //把参数转换成URL数据
 6 
 7 $aContext = array(
 8     'http' => array(
 9         'method'  => 'POST',
10 
11         'header'  => 'Content-type: application/x-www-form-urlencoded',
12 
13         'content' => $data,
14     ),
15 );
16 
17 $cxContext = stream_context_create($aContext);
18 
19 $sUrl = 'http://www.mytest.com/test.php'; //此处必须为完整路径
20 
21 $d = @file_get_contents($sUrl, false, $cxContext);
22 
23 print_r($d);
24 
25 ?>
 1 <?php
 2 /**
 3  * curl  file_get_content  fsocket 来实现post提交数据
 4  */
 5 class Request {
 6 
 7     public static function post($url, $post_data = '', $timeout = 5) {
 8         //curl
 9         $ch = curl_init();
10         curl_setopt($ch, CURLOPT_URL, $url);
11         curl_setopt($ch, CURLOPT_POST, 1);
12         if ($post_data != '') {
13             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
14         }
15         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
16         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
17         curl_setopt($ch, CURLOPT_HEADER, false);
18         $file_contents = curl_exec($ch);
19         curl_close($ch);
20         return $file_contents;
21     }
22 
23     public static function post2($url, $data) {
24         //file_get_content
25         $postdata = http_build_query($data);
26 
27         $opts = array(
28             'http' => array(
29                 'method'  => 'POST',
30                 'header'  => 'Content-type: application/x-www-form-urlencoded',
31                 'content' => $postdata,
32             ),
33 
34         );
35         $context = stream_context_create($opts);
36         $result  = file_get_contents($url, false, $context);
37         return $result;
38 
39     }
40 
41     public static function post3($host, $path, $query, $others = '') {
42         //fsocket
43         $post = "POST $path HTTP/1.1\r\nHost: $host\r\n";
44         $post .= "Content-type: application/x-www-form-";
45         $post .= "urlencoded\r\n${others}";
46         $post .= "User-Agent: Mozilla 4.0\r\nContent-length: ";
47         $post .= strlen($query) . "\r\nConnection: close\r\n\r\n$query";
48         $h = fsockopen($host, 80);
49         fwrite($h, $post);
50         for ($a = 0, $r = '';!$a;) {
51             $b = fread($h, 8192);
52             $r .= $b;
53             $a = (($b == '') ? 1 : 0);
54         }
55         fclose($h);
56         return $r;
57     }
58 }

 使用 password_hash 来哈希密码

1 <?php
2 $passwordHash = password_hash('secret-password', PASSWORD_DEFAULT);
3 if (password_verify('bad-password', $passwordHash)) {
4     // 正确
5 } else {
6     // 不正确
7 }

数据过滤

//外部输入前进行过滤和验证
filter_var();
filter_input();
// HTML 标签
strip_tags();
// 特殊字符分别进行转义从而得到各自的 HTML 实体
htmlentities();
htmlspecialchars();
// 过滤执行命令的参数
escapeshellarg();
// 最后的一个例子是接受外部输入来从文件系统中加载文件。
// 这可以通过将文件名修改为文件路径来进行利用。
// 你需要过滤掉"/", "../", null字符或者其他文件路径的字符来确保不会去加载隐藏、私有或者敏感的文件。

开发环境

为了在开发环境中显示所有可能的错误,将你的 php.ini 进行如下配置:

display_errors = On
display_startup_errors = On
error_reporting = -1
log_errors = On

缓存

PHP 本身来说是非常快的,但是但你当发起远程连接、加载文件等操作时也会遇到瓶颈。 幸运的是,有各种各样的工具可以用来加速你应用程序某些耗时的部分,或者说减少某些耗时任务所需要运行的次数。

Opcode 缓存

当一个 PHP 文件被解释执行的时候,首先是被编译成名为 opcode 的中间代码,然后才被底层的虚拟机执行。 如果PHP文件没有被修改过,opcode 始终是一样的。这就意味着编译步骤白白浪费了 CPU 的资源。

 

 

posted @ 2017-02-04 09:13  梁大师  阅读(210)  评论(0编辑  收藏  举报