存档

‘PHP’ 分类的存档

最近一直在写淘宝客的程序,因为网络的原因,file_get_contents经常读取出错,想到了比较稳定的方法,使用fsockopen连接API。fsockopen 函数在网上介绍的还是很多的,但是介绍再多,创建http请求仍然是一件比较麻烦的。
fsockopen调用的方法比较繁琐,要使用得到的数据还要去掉http头,所以冒出了写一个通用的类的方法。今天正好是51假日,在家写了这种的一个类。

<?php
/*
CatSeven myHttp Vesion 0.1

======CopyRight======
Home:http://www.myw3.cn/myDevise/myHttp/
Design:Miao Qiyuan[miaoqiyuan.cn]
Downloads:http://downloads.myw3.cn/file=myDevise/myHttp/0.1
*/

class myHttp{
public $Method,$URI,$SendDate;
public $HttpServerPort,$HttpServer,$HttpServerIP;
public $Err,$ErrStr;
public $timeout;
public $responseText;

public function __construct($uri=’/',$method=’get’,$query=”,$server=’localhost’,$port=’80′,$serverip=”,$timeout=30){
$this->URI=$uri;
$this->Method=$method;
$this->SendDate=$query;
$this->HttpServer=$server;
$this->HttpServerPort=$port;
$this->HttpServerIP=$serverip;
if(is_numeric($timeout))$this->timeout=$timeout;
}

public function send(){
$this->Method=strtoupper($this->Method);
if($this->HttpServerIP=="")$this->HttpServerIP = $this->HttpServer;
if($this->Method=="GET" && strstr($this->URI,"?")==0)$this->URI=$this->URI."?".$this->SendDate;
$sock = fsockopen($this->HttpServerIP,$this->HttpServerPort,$errno,$errstr,$this->timeout);
if(!$sock){
$this->ErrStr=$errstr;
$this->Err=$errno;
die("无法打开".$this->HttpServerIP.":".$this->HttpServerPort);
}
fwrite($sock, $this->Method." ".$this->URI." HTTP/1.0\r\n");
fwrite($sock, "Host: ".$this->HttpServer."\r\n");
if($this->Method=="POST"){
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: ".strlen($this->SendDate) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, $this->SendDate."\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "Referer: http://www.myw3.cn/myDevise/myHttp/");
}
fwrite($sock, "Connection: Close\r\n\r\n");
$headers = "";
while ($str = trim(fgets($sock,4096)))
$headers .= "$str\n";

$body = [...]

五 1st, 2010 | Filed under PHP
标签: , ,

因为数据库部分我已经写过一篇文章:给dedeims添加在线支付的功能之数据库设置,本文以该文章的数据库为准来写,如果没看过此文的网友,可以通过http://www.miaoqiyuan.cn/p/dedeims-online-pay来查看。
首先在dedeeims的会员面板中添加在线支付的链接。很简单,在menu中添加:
<li>
<h4 class=”sort”><a class=”icon par” href=”../member/pay.php”> < ?php echo GetLang('pay'); ?> </a></h4>
</li>
然后在语言文件/include/lang/下相应的语言文件中添加上对应的文字描述。
因为网银有很多,支付接口也很多,我们用的时候可能要添加多个接口,所以我们要做一个网银配置脚本:bank_config.php

< ?php
$bank_List=Array(
'网银在线'=>‘chinabank’
);
?>

首先来说dedeeims在线支付的前台,很简单。pay.php,一个简单的表单,这里不再详述,代码如下:

&lt;form class="mTB10 mL10 mR10" name="form2" action="bank.php" method="post" target="_blank">
&lt;table cellspacing="1" class="submit">
&lt;tbody>
&lt;tr>
&lt;td style="text-align:right;">支付接口:&lt;/td>
[...]

三 24th, 2010 | Filed under PHP

在织梦的论坛上看到好几篇关于自定义dedeeims的路径,不让dedeeims产品页生成静态之类的求助信息。确实,dedeeims的产品路径还带有日期,如果动态的路径,整站到再/plus/下,感觉特别不爽,今天我就给修改一下。
首先让我们感觉不爽的就是/plus/list.php?tid=这种路径作为频道(栏目,分类页),感觉特别不爽,我们就先从它下手。打开include\channelunit.func.php,找到//$reurl = $GLOBALS['cfg_phpurl'].”/list.php?tid=”.$typeid;,直接修改成$reurl = “/class.php?id=”.$typeid;这样,所有的分类页就变成了/class.php?id=…的形式了。在根目录建立一个class.php,内容如下:

< ?php
$tid=$_GET['id'];
require_once(‘plus/list.php’);
?>

很简单吧,下面修改产品展示页路径为product.php,阅读新闻页为news.php。
找到include\channelunit.func.php,function GetFileUrl($aid,$typeid,$timetag,$title,$ismake=0,$rank=0,$namerule=”,$typedir=”, $filename=”),假设产品分类为4,10,新闻分类为3,9。直接添加上如下代码:

< ?php
if($typeid==4||$typeid==10)
return ‘/product.php?product_id=’.$aid;
elseif($typeid==3||$typeid==9)
return ‘/news.php?id=’.$aid;
else
return ‘/plus/view.php?aid=’.$aid;
?>

其他情况就是默认路径了。当然也可以改成/view.php?aid=…
news.php

<?php
$aid=$_GET['id'];
require_once(‘plus/view.php’);
?>

product.php

<?php
$aid=$_GET['product_id'];
require_once(‘plus/view.php’);
?>

现在前台基本就没有问题了,后台预览文件的时候,可能会出现错误,修改admin\archives_do.php代码如下:

function viewArchives()
–>>…
if(strpos($arcurl,’?')==-1)
echo "$lt;script language=’javascript’>location.href=’$arcurl"."?".time()."’;$lt;/script>";
else
echo "$lt;script language=’javascript’>location.href=’$arcurl"."&tme=".time()."’;$lt;/script>";
exit();

三 23rd, 2010 | Filed under PHP
标签: , , ,

刚改VPS出问题了,现在刚解决好,原来打算多写几篇关于dedeims的文件,无奈现在太晚了,先写一篇 给dedeims添加在线支付的功能之数据库设置 ,页面代码等改天整理好再发。

–为会员添加余额
ALTER TABLE `dede_member` ADD `money` DOUBLE(11,2) Default 0 NOT NULL AFTER `scores`
ALTER TABLE `dede_member` ADD `moneyto` DOUBLE(11,2) Default 0 NOT NULL AFTER `money`

–添加充值记录
–paytype 0:在线充值;1:提现;2:积分兑换
DROP TABLE IF EXISTS `dede_paylog`;
CREATE TABLE IF NOT EXISTS `dede_paylog` (
`id` mediumint(8) NOT NULL auto_increment,
`mid` mediumint(8) NOT NULL,
`paytype` smallint(5) NOT [...]

三 23rd, 2010 | Filed under Experience(经验), PHP, Share(分享)

phpCache,现在更新到了0.11。
2010-1-31 Vesion 0.11
修复了每次读取字符不能超过128KB的限制
去除了nocahce模式存储的文件的功能
增加了读取失败的错误提示符
有需要的朋友请到http://www.catseven.cn/phpCache/下载。

< ?php
/*
CatSeven phpCache Vesion 0.11

======CopyRight======
Home:http://www.catseven.cn/phpCache/
Design:Miao Qiyuan[miaoqiyuan.cn]
*/

class cache404{
public $K,$P,$V;
public function __construct($URI,$P='blog',$Par='./404cache/'){
$this->U = $URI;
$this->K = md5($URI);
$this->P = $P;
$this->Par = $Par;
$this->E = ‘.tmp_miaoqiyuan’;
$this->F = $this->Par . $this->P . ‘/’ . $this->K . $this->E;
$this->V = ‘CatSeven phpCache Error.’;
}
public function getHtml($u){
return file_get_contents($u,NULL,NULL,-1,1000000);
}
public function getCache(){
if(!!($fp=@fopen($this->F,’r'))){
$html=fread($fp,102400);
}else{
if($html=$this->getHtml($this->U)){
$this->addIndex();
$fp=fopen($this->F,’w');
fwrite($fp,$html);
}else{
$html=$this->V;
}
}
if($fp)fclose($fp);
return $html;
}
public function noCache(){
if(!$html=$this->getHtml($this->U))$html=$this->V;
return $html;
}
public function flushCache(){
unlink($this->F);
}
public function addIndex(){
$fpidx=fopen($this->Par . $this->P . ‘.rtf’,'a’);
fwrite($fpidx,$this->K . [...]

二 1st, 2010 | Filed under PHP, Share(分享)
标签: , ,

前几天写过一篇巧用404.php解决Wordpress耗资源的问题,给Wordpress加个缓存功能(http://www.miaoqiyuan.cn/p/wordpress-haoziyuan-wordpress-cache)中提到了PHP缓存的方法,总感觉不是很方便,现在我又写了一个新的。

< ?php
class cache404{
public $K,$P;
public function __construct($URI,$P='blog',$Par='./cache/'){
$this->U = $URI;
$this->K = md5($URI);
$this->P = $P;
$this->Par = $Par;
$this->E = ‘.tmp_miaoqiyuan’;
$this->F = $this->Par . $this->P . ‘/’ . $this->K . $this->E;
}
public function getCache(){
if(!!($fp=@fopen($this->F,’r'))){
$html=fread($fp,102400);
}else{
$html=file_get_contents($this->U);
$this->addIndex();
$fp=fopen($this->F,’w');
fwrite($fp,$html);
}
fclose($fp);
return $html;
}
public function noCache(){
$html=file_get_contents($this->U);
$fp=fopen($this->F,’w');
fwrite($fp,$html);
fclose($fp);
return $html;
}
public function flushCache(){
unlink($this->F);
}
public function addIndex(){
$fpidx=fopen($this->Par . $this->P . ‘.rtf’,'a’);
fwrite($fpidx,$this->K . ‘ ‘ .$this->U);
fclose($fpidx);
}
}
?>

用的时候很简单

require_once(‘./inc/404cache.php’);
$Cache=new cache404($URI,$Path);
#echo $Cache->noCache();
echo $Cache->getCache();

< ?php
require_once('./inc/404cache.php');
$q=explode("/",$URI=substr(($qs = strtolower($_SERVER['QUERY_STRING'])),strpos($qs, ':80')+4));
$URI="/".$URI;
switch($q[0]){
case 'test':
#/test -> /seo_test/test.asp
if(preg_match(‘/^\/test(\/)?$/’,$URI)){
$URI=’http:’.'//’.$_SERVER['SERVER_NAME'].’/seo_test/test.asp’;
}

#/test/[a-z]\.html -> /seo_test/test.asp?key=$1
else [...]

一 22nd, 2010 | Filed under Experience(经验), PHP, Share(分享), Thinks(想法)
标签: , , ,