标签归档:php

抓取上海教育考试院的报名信息并短信通知

由于有过惨痛教训,老是错过重要的考试报名通知,所以这段代码抓取了上海教育考试院的报名页面,分析并发送短信通知相应的考试人员,然后将这个页面放到服务器上,每日定时检查,一有新的报名信息就能及时得知。中间涉及到部分php的知识点

    1. fscoketopen,上一篇已经写过相应的介绍,即可以用其来模拟web service调用,也可以使用其来抓取页面
    2. 正则表达式,preg_match,用来提取抓取后页面返回的数据
    3. 编码转换iconv,抓取的页面是GBK类型,如果不转换,则在控制台显示乱码。第二个是发送短信的接口,由于我使用的接口是GBK数据,所以我需要再从UTF-8转换到GBK
    4. php文件的相关操作函数:file_exists用来判断文件是否已经存在,fopen用来打开文件,fgets用来读取一行,fputs用来写入一行。
    5. 数组模拟push方法,使用arr[] = something;的形式
    6. vim 格式化代码 :gg=G
    7. vim 批量添加 //注释::10,50s#^#//#g ;批量删除 :10,50s#^//##g
    8. 定时的方法使用crontab -e,就可打开定时列表,设置成每天10点通知:
0 10 * * * /path/to/php /path/to/spta.php

spta.php

function get_spta() {
    $content = '';

    $fp = fsockopen('www.spta.gov.cn', 80);
    fwrite($fp, "GET /appendix/wsbm.html HTTP/1.0\r\n");
    fwrite($fp, "Host: www.spta.gov.cnrn");
    fwrite($fp, "Content-Type: text/html; charset=utf-8\r\n");
    fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
    fwrite($fp, "\r\n");

    fwrite($fp, $content);

    $item = array();
    while (!feof($fp)) {
        $result = iconv('GBK', 'UTF-8', fgets($fp));
        if (strpos($result, '<td align="left">') > 0) {
            preg_match('/>([^<]*)</', $result, $matches);
            $title = $matches[1];
            $url = iconv('GBK', 'UTF-8', fgets($fp));
            preg_match('/href="([^"]*)"/', $url, $matches);
            $url = $matches[1];
            $item[] = array('title' = >$title, 'url' = >$url);
        }
    }
    fclose($fp);
    return $item;
}
function sent_sms($mobile, $msg) {
    $vars = "&mobs=$mobile&msg=".iconv('UTF-8', 'GBK', $msg);
    $fp = fsockopen('smsserver.com', 80);
    fwrite($fp, "GET /sms?$vars HTTP/1.0\r\n");
    fwrite($fp, "Host: smsserver.com\r\n");
    fwrite($fp, "\r\n");

    fwrite($fp, $content);
    fclose($fp);
}
$items = get_spta();
$title = '';
$path = '/path/to/spta';
if (file_exists($path)) {
    $file = fopen($path, 'r');
    $title = fgets($file);
}
foreach($items as $bean) {
    if ($title != $bean['title']) {
        sent_sms('your mobile', $bean['title'].'[考试院]');
        echo $bean['title']."n";
    } else {
        break;
    }
}
$file = fopen($path, 'w');
fputs($file, $items[0]['title']);

使用php fsockopen 调用 .NET的WebService接口

实现代码如下所示,我们知道了可以使用Telnet模拟http访问,也想试着使用socket来调用webservice接口,原因是默认的php Soap不起作用,可能需要繁琐的配置才能实现。但在使用fsockopen调用的时候也现出了一些问题:

    1. php中,单引号中是没有转义字符的,即\r\n是原始显示的
    2. http头部使用1.1的时候,可能会出现keep-alive模式,这种情况下需要使用Content-Length来判断body的长度,取出数据结束,而不应使用feof
    3. fgets()是一次读取一行;fread()是一次读取一个字符,后面可加数量
$content = '<?xml version="1.0" encoding="utf-8"?>'."\r\n";
$content. = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'."\r\n";
$content. = '  <soap:Body>'."\r\n";
$content. = '    <QueryBalance  xmlns="http://tempuri.org/">'."\r\n";
$content. = '      <userId>sg128</userId>'."\r\n";
$content. = '    </QueryBalance >'."\r\n";
$content. = '  </soap:Body>'."\r\n";
$content. = '</soap:Envelope>'."\r\n";

$fp = fsockopen('example.com', 80);
fwrite($fp, "POST /Service/UserService.asmx HTTP/1.0\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: text/xml; charset=utf-8\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "SOAPAction: \"http://tempuri.org/QueryBalance\"\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');

// $length = 0;
// $line = '';
// while($line !== "\r\n") {
//     $line = fgets($fp);
//     if(substr($line, 0, 15) === 'Content-Length:') {
//         $length = intval(substr($line, 16));
//     }
// }
// echo fread($fp, $length);
while (!feof($fp)) {
    echo fgets($fp);
}
fclose($fp);

Use of undefined constant OCI_COMMIT_ON_SUCCESS – assumed ‘OCI_COMMIT_ON_SUCCESS’

安装完wampServer,然后勾选了oci8.dll,codeigniter还是不能启动,报错Use of undefined constant OCI_COMMIT_ON_SUCCESS – assumed ‘OCI_COMMIT_ON_SUCCESS’,如下图

image

使用oci_connect方法的时候会报找不到这个方法的错误

image

在默认环境变量里面能找到如下dll

image

有如下解决方法(三选一)

  1. 下载并Oracle客户端,Oracle会自动在path里面加入D:\app\niehonglei\product\11.2.0\client_1\bin;
  2. 下载instantclient,然后在path里面把instantclient路径加进去;
  3. 下载instantclient,将其中的dll拷贝到C:\Windows\System32目录里面;

codeigniter mysql php相关总结

开发lcwy这个项目,项目地址:http://lcwy.tohours.com,学习了不少php相关知识,做了如下总结:

  1. <?php require_once(“application/views/script/split_page.php”);?> 引用相关php文件
  2. 先$sql = $sql.” order by t.insert_time desc”; 后$sql = $sql.” limit “.$offset.”,”.$limit;
  3. mysql的 $offset 从0开始
  4. $this -> input -> cookie();可以取得相应的cookie
  5. foreach($_POST as $key => $value) 数组的遍历方法
  6. 查询方法 $this -> db -> where(array(“condition” => “value”)) -> order_by() -> get(“table_name”) -> result(); 取得对象的数组
  7. 也可使用 -> row()方法取得第一行的数据 (替换-> get() –> result())
  8. 也可使用 -> count_all_results()计算数据的条数,而不得到数据 (替换-> get() –> result())
  9. sql查询方法 $this -> db -> query($sql, $param) -> result();后面能接的方法与上面相同
  10. 更新方法$this -> db -> where(“id”, $id) -> update(“user”,$user);
  11. 字符串替换方法 str_replace(“world”,”earth”,”Hello world!”); //Hello earth!
  12. 正则替换方法 preg_replace(“/^[0-9]{4}/”, “”, $year); 其中正则也是以/开头和结尾
  13. 计算数据长度使用count(数组名称)
  14. 数字转字符 字符转数字函数 echo chr(65);echo ord(“B”);
  15. echo strpos(“Hello world!”,”wo”)
  16. 输出$this -> output -> set_output(encode_json($json));
  17. explode(“,”, $ids); split功能
  18. $data[“clear_time”] = null;//将数据库时间置空
  19. date_default_timezone_set(‘PRC’);在使用time时预先设置

CodeIgniter初始化需要调整的文件

使用CI开发时,需要对原来的框架配置文件做少量调整,以便我们使用。由于开发时间过长,现在只能从svn调整的记录里面来找到这些东西,可能有些地方一时记不起来为什么要调整了,记录下来下次初始化时好用

1、autoload.php,加载我们需要的选项,共有两个需要调整的地方

image

2、config.php两个地方,上面用来固定路径,后面用来加密

image

image

 

3、database.php,根据自己的数据库配置情况来调整

image

ORACLE配置示例:

// oracle
$db['default']['hostname'] = '(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =host.name.or.ip)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = service.name)))';
$db['default']['username'] = 'username';
$db['default']['password'] = '111111';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'oci8';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

4、routes.php将默认的首页进行调整(可选,一般不建议调整成index)

image

另外,如果是配置oracle数据库,请将system\database\drivers\oci8\oci8_result.php文件修改一下,不然会报警告

image

5、子目录配置htaccess方案

<IfModule mod_rewrite.c>
    RewriteEngine on RewriteBase /sppt/ RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ ./index.php/$1 [L]
</IfModule>

6、在每个controller前加上这段代码

function __construct(){
    parent::__construct();
}

7、开启apache的rewrite模块

#LoadModule rewrite_module modules/mod_rewrite.so