curl 抓取跳转内容

December 7th, 2011 陈毓端 No comments

同事在用curl抓取图片时遇到nginx rewrite redirect 跳转,这时 curl -o a/xx.png http://xxx.xxx.com/190/180/ 无法抓取跳转后的内容

正确的做法, 添加-L:
curl -o a/xx.png -L http://xxx.xxx.com/190/180/

-L/–location Follow Location: hints (H)
–location-trusted Follow Location: and send auth to other hosts (H)

Categories: linux Tags:

var_dmup 源码分析 告诉你var_dmup(1!=2)的为什么是TRUE

October 26th, 2011 陈毓端 1 comment

今天QQ群里一个兄弟问 var_dump(“$key” != “testid”);
1 是什么结果?
2 后来问为什么是true?

我们来看看PHP是如何实现函数var_dump

  1. PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC) /* {{{ */
  2. {
  3.     HashTable *myht;
  4.     char *class_name;
  5.     zend_uint class_name_len;
  6.     int (*php_element_dump_func)(zval** TSRMLS_DC, int, va_list, zend_hash_key*);
  7.     int is_temp;
  8.  
  9.     if (level > 1) {
  10.         php_printf("%*c", level - 1, ' ');
  11.     }
  12.  
  13.     switch (Z_TYPE_PP(struc)) {
  14.     case IS_BOOL:
  15.         php_printf("%sbool(%s)\n", COMMON, Z_LVAL_PP(struc) ? "true" : "false");
  16.         break;
  17.     case IS_NULL:
  18.         php_printf("%sNULL\n", COMMON);
  19.         break;
  20.     case IS_LONG:
  21.         php_printf("%sint(%ld)\n", COMMON, Z_LVAL_PP(struc));
  22.         break;
  23.     case IS_DOUBLE:
  24.         php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc));
  25.         break;
  26.     case IS_STRING:
  27.         php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc));
  28.         PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
  29.         PUTS("\"\n");
  30.         break;
  31.     case IS_ARRAY:
  32.         myht = Z_ARRVAL_PP(struc);
  33.         if (++myht->nApplyCount > 1) {
  34.             PUTS("*RECURSION*\n");
  35.             --myht->nApplyCount;
  36.             return;
  37.         }
  38.         php_printf("%sarray(%d) {\n", COMMON, zend_hash_num_elements(myht));
  39.         php_element_dump_func = php_array_element_dump;
  40.         is_temp = 0;
  41.         goto head_done;
  42.     case IS_OBJECT:
  43.         myht = Z_OBJDEBUG_PP(struc, is_temp);
  44.         if (myht && ++myht->nApplyCount > 1) {
  45.             PUTS("*RECURSION*\n");
  46.             --myht->nApplyCount;
  47.             return;
  48.         }
  49.  
  50.         Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
  51.         php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0);
  52.         efree(class_name);
  53.         php_element_dump_func = php_object_property_dump;
  54. head_done:
  55.         if (myht) {
  56.             zend_hash_apply_with_arguments(myht TSRMLS_CC, (apply_func_args_t) php_element_dump_func, 1, level);
  57.             --myht->nApplyCount;
  58.             if (is_temp) {
  59.                 zend_hash_destroy(myht);
  60.                 efree(myht);
  61.             }
  62.         }
  63.         if (level > 1) {
  64.             php_printf("%*c", level-1, ' ');
  65.         }
  66.         PUTS("}\n");
  67.         break;
  68.     case IS_RESOURCE: {
  69.         char *type_name;
  70.  
  71.         type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC);
  72.         php_printf("%sresource(%ld) of type (%s)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown");
  73.         break;
  74.     }
  75.     default:
  76.         php_printf("%sUNKNOWN:0\n", COMMON);
  77.         break;
  78.     }
  79. }

var_dump 定义了 8 中数据类型
【IS_BOOL】
【IS_NULL】
【IS_LONG】
【IS_DOUBLE】
【IS_STRING】
【IS_ARRAY】
【IS_OBJECT】
【IS_RESOURCE】
为什么是true? 关键在 Z_LVAL_PP :zend api 获取类型

  1. case IS_BOOL:
  2.         php_printf("%sbool(%s)\n", COMMON, Z_LVAL_PP(struc) ? "true" : "false");

还实现了【IS_ARRAY】类型的head_done 处理遍历方法。

Categories: c, php, PHP 内核/源码 Tags:

php 使用 oracle oci8 配置

September 26th, 2011 陈毓端 1 comment

以centos 64位为例

下载:
进入oracle 官方资源 http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
oracle-instantclient11.2-basic-11.2.0.2.0.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.2.0.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.2.0.x86_64.rpm

安装:
rpm -ivh oracle-instantclient11.2-basic-11.2.0.2.0.x86_64.rpm oracle-instantclient11.2-devel-11.2.0.2.0.x86_64.rpm oracle-instantclient11.2-sqlplus-11.2.0.2.0.x86_64.rpm

进入php源码目录:
cd /soft/php-5.3.8/ext/oci8/
./configure –with-php-config=/usr/local/php5/bin/php-config –with-oci8=instantclient,/usr/lib/oracle/11.1/client64/lib/
make && make install
vim php.ini
extension=oci8.so
重启php 查看phpinfo
OCI8 Support enabled

  1. <?php
  2. $conn = oci_connect('用户名', '密码', '//xxx.com:端口/XE5',"ZHS16GBK");
  3. if (!$conn) { 
  4.     $e = oci_error();
  5.     trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  6. }
  7. ?>
Categories: php Tags:

linux 扩充内存,添加内存上限

July 28th, 2011 陈毓端 No comments

我的ubunut 10.04 32位内存上限为4G. 要加到6G必须进行升级
sudo apt-get install linux-generic-pae
sudo apt-get install linux-headers-generic-pae

解释下什么是pae:
物理地址扩展是内存的一种技术.详见:

https://help.ubuntu.com/community/EnablingPAE

Categories: Uncategorized Tags:

cannot find -lltdl

July 20th, 2011 陈毓端 No comments

yum install libtool-ltdl-devel

Categories: linux, php Tags:

linux pdf 切割 工具pdftk

April 5th, 2011 陈毓端 3 comments

android 系统阅读大点的PDF文件总是很卡,推荐PDF切割工具pdftk。
切割单页模式:pdftk xxx.pdf burst

下面是一个按照页数切割脚本

  1. #!/bin/bash
  2. pdfQG () {
  3. i=1
  4. t=0
  5. pnum=$2 #总页数
  6. sp=$3; #每个pdf文件页数
  7. while [ $i -le  $pnum ];
  8. do
  9. t=$[ i + sp - 1 ] 
  10.  
  11. if [ $[ i + sp ] -gt $pnum ]
  12. then pdftk $1 cat $i-end output partend.pdf
  13. else pdftk $1 cat $i-$t output part$t.pdf
  14. echo "pdftk $1 cat $i-$t output part$t.pdf"
  15. fi
  16. i=$[ i + sp ]
  17. done
  18. }
  19.  
  20. # 调用
  21. pdffile=$1;
  22. pdfnum=$2;
  23. pdfpage=$3;
  24. if [  -z $pdffile ];then 
  25. echo "请输入PDF文件";
  26. elif [ -z $pdfnum ];then 
  27. echo "输入要PDF文件页数";
  28. elif [ -z $pdfpage ];then 
  29. echo "输入要分割的单个文件页数";
  30. else 
  31. pdfQG $pdffile $pdfnum $pdfpage
  32. fi

ok 执行
./pdfQG.sh pdf文件地址 文件总页数 切割每个文件页数

Categories: linux Tags:

linux平台下lazarus 调用执行文件

March 26th, 2011 陈毓端 No comments

1 需要引入 Process
2

  1. procedure TSendNewThread.SendMsg;
  2. var   pC:  TProcess;
  3. begin
  4. pC:=TProcess.Create(nil);
  5. pC.CommandLine:='./curls 1 '+ fun.EncryptString(Form1.Memo1.Text,'12345cydphpim');//
  6. pC.Options:=pC.Options+[poWaitOnExit, poUsePipes];
  7. pC.Execute;
  8. pC.Free;
  9. end;
Categories: delphi, linux Tags: ,

小试sina微博接口(c语言版)

March 19th, 2011 陈毓端 No comments

最近有朋友开发的基于sina 微博api被中止授权,引用他们的说法是”伪开放”。今天看了sina的api,在不谈论开放程度和决策层的前提下,从技术成面分析不可否认是国内目前开放API平台做的最好的,无论是document还是数据统计都不错。

我小试了2个接口.开发环境 linux c curl
sina_mic_blog.h

  1. #include "stdio.h"
  2. #include "curl/curl.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "dlfcn.h"
  6. /*
  7. *sendMicBlog 发送文字
  8. */
  9. void sendMicBlog(char *userpwd,char *msg,char *stype,char *appkey);
  10. /*
  11. *sendPicBlo 发送图片+文字
  12. */
  13. void sendPicBlog(char *userpwd,char *pic,char *msg,char *appkey);

sina_mic_blog.c

  1. #include "sina_mic_blog.h"
  2. void sendMicBlog(char *userpwd,char *msg,char *stype,char *appkey)
  3. {
  4.     CURL *curl;
  5.     CURLcode res;
  6.     char *s="&status=";
  7.     char *s1="&annotations=";
  8.     char pp[200];
  9.     char curlURL[100]="http://api.t.sina.com.cn/statuses/update.xml?source=";
  10.     strcat(pp,s);
  11.     strcat(pp,msg);
  12.     strcat(pp,s1);
  13.     strcat(pp,stype);
  14.     strcat(curlURL,appkey);
  15.     curl = curl_easy_init();
  16.     if(curl)
  17.     {
  18.         curl_easy_setopt(curl, CURLOPT_URL,curlURL);
  19.         curl_easy_setopt(curl, CURLOPT_POSTFIELDS,pp);
  20.         curl_easy_setopt(curl, CURLOPT_USERPWD,userpwd);
  21.         res = curl_easy_perform(curl);
  22.         curl_easy_cleanup(curl);
  23.     }
  24.  
  25. }
  26. void sendPicBlog(char *userpwd,char *pic,char *msg,char *appkey)
  27. {
  28.     CURL *curl;
  29.     CURLcode res;
  30.     CURLM *multi_handle;
  31.     int still_running;
  32.     struct curl_httppost *formpost=NULL;
  33.     struct curl_httppost *lastptr=NULL;
  34.     struct curl_slist *headerlist=NULL;
  35.     static const char buf[] = "Expect:";
  36.  
  37.     curl_formadd(&formpost,
  38.                &lastptr,
  39.                CURLFORM_COPYNAME, "pic",
  40.                CURLFORM_FILE, pic,
  41.                CURLFORM_FILENAME, "pic",
  42.                CURLFORM_END);
  43.  
  44.   curl_formadd(&formpost,
  45.                &lastptr,
  46.                CURLFORM_COPYNAME, "status",
  47.                CURLFORM_COPYCONTENTS,msg,
  48.                CURLFORM_END);
  49.  
  50.  
  51.     char curlURL[200]="http://api.t.sina.com.cn/statuses/upload.xml?source=";
  52.  
  53.     strcat(curlURL,appkey);
  54.     curl = curl_easy_init();
  55.     multi_handle = curl_multi_init();
  56.     headerlist = curl_slist_append(headerlist, buf);
  57.     if(curl)
  58.     {
  59.         curl_easy_setopt(curl, CURLOPT_URL,curlURL);
  60.         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  61.         curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  62.         curl_multi_add_handle(multi_handle, curl);
  63.         curl_multi_perform(multi_handle, &still_running);
  64.         curl_easy_setopt(curl, CURLOPT_USERPWD,userpwd);
  65.         res = curl_easy_perform(curl);
  66.         curl_easy_cleanup(curl);
  67.     }
  68. }
  69.  
  70. int main(int argc, char *argv[])
  71. {
  72.  
  73.     char *userpwd;
  74.     /*
  75.     send type
  76.     */
  77.     int t;
  78.     t=atoi(argv[1]);
  79.  
  80.     char *stype;    //发送类型 1 1文字 2 图片+文字
  81.     char *appkey=argv[2];
  82.     stype="[{\"type2\":123}]";
  83.     userpwd=argv[3]//用户名:密码
  84.     switch(t)
  85.     {
  86.     case 1:
  87.         sendMicBlog(userpwd,argv[4],stype,appkey);//
  88.         break;
  89.     case 2:
  90.         sendPicBlog(userpwd,argv[4],argv[5],appkey);//
  91.         break;
  92.     default :
  93.         sendMicBlog(userpwd,argv[4],stype,appkey);//
  94.         break;
  95.     }
  96.  
  97.     return 1;
  98. }

编译: gcc -o sina_mic_blog -Wall sina_mic_blog.c -lcurl
发送文字: sina_mic_blog 1 申请的appkey 用户名:密码 发送的内容
发送图片+文字: sina_mic_blog 2 申请的appkey 用户名:密码 图片路径 发送的文字内容

Categories: c, CYDPHP, linux, 编程语言 Tags: , , ,

nginx 实现 apache .htaccess

September 7th, 2010 陈毓端 2 comments

今天一个技术前辈问我Nginx是否可以实现apache 的 .htaccess 功能。 因为之前在网络上看过类似的文章但没有实验过(不喜欢这个机制)。我不在意的回答yes。其实nginx是不支持.htaccess或则说是没有内置这么一套机制。
要实现.htaccess的这个功能,先了解下.htaccess 的执行逻辑。
很简单。Apache只要AllowOverride 开启支持.htaccess,就会在每次执行的目录中扫描(.htaccess)文件(^..^多么无语的机制).
当然 apache是每次扫描.htaccess。而nginx 是肯定不能每次扫描这个.htaccess文件了。所以只能将.htaccess 在启动是装入nginx配置。
.htaccess的特点是每次不需要重启apache就可以实现规则控制。
针对这个特点可以利用linux内核中的文件系统变化通知机制来实现动态更新nginx的配置。
1 安装inotify
参考IBM的文章(http://www.ibm.com/developerworks/cn/linux/l-inotify.html)
2 安装 inotify-tools

  1. wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
  2. tar zvxf inotify-tools-3.14.tar.gz
  3. cd inotify-tools-3.14
  4. ./configure
  5. make && make install

编写.htaccess 监听脚本
vim nginxhtaccess.sh

  1. #!/bin/sh
  2. inotifywait -m -e modify  /usr/local/nginx/html/.htaccesswhile read file
  3.  do
  4.   /usr/local/nginx/sbin/nginx -s reload 
  5. Done

监听/usr/local/nginx/html/ 目录下的.htaccess 文件修改事件。当被修改后就以不暂停nginx的方式将.htaccess规则写入nginx。
如果有多个.htaccess文件 可以用

  1. #!/bin/sh
  2. inotifywait -m -e modify --fromfile /usr/local/nginx/html/allhtaccess.txt | while read file
  3.  do
  4.   /usr/local/nginx/sbin/nginx -s reload 
  5. Done

汇总一个.htaccess 规则文件的地址 allhtaccess.txt
–fromfile /usr/local/nginx/html/allhtaccess.txt

3 修改nginx.conf 的配置文件
添加

  1. include /usr/local/nginx/html/.htaccess; //.htaccess 文件地址

4 启动nginxhtaccess.sh 监听脚本

  1. ./nginxhtaccess.sh &

简单的实现完毕。

CYDPHP一键集成环境(一键PHP安装包) 1.0 Nginx+MySQL+PHP-CGI+Memcached 集成B/S开发工具箱

August 12th, 2010 陈毓端 33 comments

CYDPHP 一键集成环境是什么?

Construct Yare Development 构建敏捷开发环境

CYDPHP 绿色 免安装 针对Windows下快速开发PHP及B/S开发模式下日常必备功能的一键整合开发环境。

CYDPHP 功能组件

  • PHP 5.3.3
  • Nginx 0.8.39
  • MySQL 5.1.48
  • memcache 2.2.5
  • eAccelerator 0.9.6
  • Xdebug 2.1.0
  • phpMyAdmin mysql管理
  • AB 压力测试
  • javaScript 压缩工具
  • CSS 压缩工具
  • PHP 代码格式美化
  • MySQL 数据批量生成工具
  • xdebug分析工具
  • SFTP/FTP
  • Windows 常用命令
  • DOS Linux shell 模拟工具
  • 端口进程分析工具
  • 翻译工具
  • 取色工具
  • 快捷文档软件管理工具

CYDPHP 事项

  • 需解压到非中文目录下,否则部分功能无法使用(路径不能包含空格)。
  • 具有端口和进程检测功能。
  • MySQL启动选择(可以选择系统已安装的MySQL)
  • 采用多线程机制,保证部分功能的同时进行

联系方式

软件作者:  陈毓端
围脖: http://t.qq.com/cydphp
MSN:   yuduan2009@hotmail.com
E-mail:   thisduan@gmail.com
下载地址: 【新浪下载】 【天空下载