« Posts tagged PHP

PHP connect AD with LDAP over SSL (LDAPs)

《Server is unwilling to perform》之后的三个多月,终于有了测试环境。节后的第一个工作日,用了一个下午的时间进行测试,终于成功了。

主要参考文档为adldapLDAP over SSL

环境:win2003 server 用于安装ad和ca,ubuntu做为webserver,安装apache2和php

准备工作:运行phpinfo(),确认php环境已经支持openssl和openldap,关于php环境的配置不多说了。

step 1:安装CA,用来颁发证书,微软或者第三方的都可以,我装的微软的CA,这里很简单

参考文档:安装windows server 2003 证书服务(CA) 企业数字证书认证服务

setp 2:创建证书,并使ad下的ldap开启对ssl的支持,配置完成之后可以使用ldp.exe来测试ldaps连接是否成功,这里也很简单,照着文档做就OK。 »Read More

Server is unwilling to perform

在使用php连接ldap向ad中添加用户的时候遇到问题:“Server is unwilling to perform”,搜索之后发现相关资料不多,并且——解释五花八门。最后得到了同事的启发,发现这个报错与用户密码有关,把密码参数去掉后创建用户的操作返回了true,再看AD中的这个用户的状态却是禁用的。有点不理解,但总算有了些进展。

(续)上周又有了新的进展,但一直没时间更新日志。前几天找到一篇Loudly前辈的文章,链接地址:http://blog.loudly.cn/2007/11/4bfe75d9de3096a90055876934b77f21/ 。文章中提到的工作环境跟我的现在很相似,基本上可以确认不能操作用户密码的问题与SSL有关,但由于一些限制我现在没有办法进行测试,测试的工作会在最近的1-2周内完成,到时候再把我的测试结果发上来吧。

再续

PHP connect AD with LDAP over SSL (LDAPs)

ldap_search(): Partial search results returned: Sizelimit exceeded

        最近在研究用php ldap连接active directory(主要是使用adLDAP类),在使用folder_list()方法递归OU的时候发现报错,即ldap_search(): Partial search results returned: Sizelimit exceeded——只能查询1000个对象,google了一下,发现ad ldap默认的策略是有限制的(估计是考虑到性能的因素),将默认的MaxPageSize参数修改为一个较大的值即可(例如3000,我的测试环境里2000就够用了),修改方法如下。

Viewing current policy settings

  1. At the Ntdsutil.exe command prompt, type LDAP policies, and then press ENTER.
  2. At the LDAP policy command prompt, type connections, and then press ENTER.
  3. At the server connection command prompt, type connect to server <DNS name of server>, and then press ENTER. You want to connect to the server that your are currently working with.
  4. At the server connection command prompt, type q, and then press ENTER to return to the previous menu.
  5. At the LDAP policy command prompt, type Show Values, and then press ENTER.
    A display of the policies as they exist appears.

»Read More

用php抓取汇率和libor

数据来源:中国资金管理网

功能:抓取前一交易日的libor和当天的汇率

PHP
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
<?php
    //leon.nerr@gmail.com
    //2009-07-27
    header("Content-Type: text/html; charset=utf-8"); 
    function getArr($url,$date){
        $str = file_get_contents($url);
        $str = iconv('gb2312','utf8',$str);//
        //去html,js,css,iframe
        $str = preg_replace( "@<script(.*?)</script>@is", "", $str ); 
        $str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str ); 
        $str = preg_replace( "@<style(.*?)</style>@is", "", $str ); 
        $str = preg_replace( "@<(.*?)>@is", "", $str );
        $str = substr($str,strpos($str,$date),1000);
        if(strlen($str)>0){
            $arr = explode(' ',$str);
            foreach($arr as $key=>$value){
                if(strlen(trim($value))>0){
                    $result[] = trim($value);
                }   
            }    
        }
        return $result;
    }
    //
    if(date('N')==1){
        $yesterday = date('Y-m-d',strtotime("-3 days"));    
    }else{
        $yesterday = date('Y-m-d',strtotime("-1 days"));
    }
    $today = gmdate('Y-m-d');
    //
    $result = getArr("http://www.treasurer.org.cn/modules/wzjjgl/libor_index.php",$yesterday);
    if(is_array($result)){
        $libor['DATE'] = $result[0];
        $libor[1] = $result[6];
        $libor[3] = $result[10];
        $libor[6] = $result[16];
        $libor[12] = $result[28];
    }
    $result = getArr("http://www.treasurer.org.cn/",$today);
    if(is_array($result)){
        $rate['DATE']= $result[0];
        $rate['USD'] = $result[6];
        $rate['ERU'] = $result[12];  
    }
    //
    echo '<pre>';
    print_r($libor);
    print_r($rate);
    echo '<pre>';
?>

后来发现了一个叫snoopy的类,貌似很好用,有空一定要试一下

从Google读取当前人民币汇率中间价

PHP
<?php
    function findstr($line,$condi,$num){
        $a = strpos($line, $condi);
        $arr = explode(' ', substr($line, $a+$num, 20));
        return $arr[0];
    }
 
    $url = "http://www.google.com/search?&q=1usd%3D%3Frmb"; //usd
    $str = file_get_contents($url);
    preg_match_all("/1 U.S. dollar = .* Chinese yuan/", $str, $arr);
    $rate['usd'] = findstr($arr[0][0], '= ', 2);
 
    $url = "http://www.google.com/search?&q=1eur%3D%3Frmb"; //eur
    $str = file_get_contents($url);
    preg_match_all("/1 Euro = .* Chinese yuan/", $str, $arr);
    $rate['eur'] = findstr($arr[0][0], '= ', 2);
 
    print_r($rate);
?>

PHP中$_SERVER的详细用法

$_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关。
$_SERVER['argv'] #传递给该脚本的参数。
$_SERVER['argc'] #包含传递给程序的命令行参数的个数(如果运行在命令行模式)。
$_SERVER['GATEWAY_INTERFACE'] #服务器使用的 CGI 规范的版本。例如,“CGI/1.1”。
$_SERVER['SERVER_NAME'] #当前运行脚本所在服务器主机的名称。
$_SERVER['SERVER_SOFTWARE'] #服务器标识的字串,在响应请求时的头部中给出。

»Read More