PHP

浏览模式: 标准 | 列表 | 图文
微信菜单的实现代码

微信菜单的实现代码

不多说,上代码。有几个要注意的(这段代码是我从我的系统里剥出来的。写了很久了,只是最近有人一直在问,我想,这也不是什么特别的代码,还是开源吧。)

1、请求微信菜单需要token,所以要事先生成。这个token其实可以用不少时间的,看官方接口(token调用生成,每天是有次数的,所以节约着用,能存本地就存本地吧,记录下过期时间即可)
2、子菜单的数组顺序就是微信的子菜单:从上至下的顺序 。这个不要搞错
3、子菜单是有数量限制的
4、主菜单是有字数限制的。不能超过4个中文字。
上代码吧:
<?php
/**
* @category menu.php
* @author gouki <gouki.xiao@gmail.com>
* @created 2013-10-16 15:14
* @since
*/
$menudata = array(
'button' => array(
array(
'name' => '菜单一',
'sub_button' => array(
array(
'type' => 'click',
'name' => '最上方子菜单1',
'key' => 'R:4:1',
),
array(
'type' => 'click',
'name' => '下方菜单',
'key' => 'R:3:1',
),
)
),
array(
'name' => '菜单2',
'sub_button' => array(
array(
'type' => 'click',
'name' => '第一个',
'key' => 'R:2:1',
),
array(
'type' => 'click',
'name' => '第二个',
'key' => 'R:8:1',
),
array(
'type' => 'click',
'name' => '第三个',
'key' => 'R:9:1',
),
array(
'type' => 'click',
'name' => '第四个',
'key' => 'R:10:1',
),
)
),
array(
'type' => 'click',
'name' => '菜单3',
'key' => 'M:7',
),
)
);
$menujson = json_encode($menudata, JSON_UNESCAPED_UNICODE);
define('APPKEY', ''); //这里是APPKEY
define('SECRET', ''); //SECRET ....
$getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
$getTokenUrl = sprintf($getTokenUrl, APPKEY, SECRET);
$result = json_decode(file_get_contents($getTokenUrl), true);
$token = '';
if (isset($result['access_token'])) {
$token = $result['access_token'];
}
if (!$token) {
throw new Exception('token can not empty');
}

$createMenuUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $token;
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencodedContent-Length: " . strlen($menujson) . "" . "Connection: keep-alive" . "Keep-Alive: " . 300 . "",
'content' => $menujson,
'timeout' => 120,
),
);

$context = stream_context_create($opts);
$response = json_decode(file_get_contents($createMenuUrl, false, $context),true);

if(!$response['errcode'] ){
echo "success";
}else{
echo $response['errmsg'];
}

echo "<pre>";
print_r($response);
echo "</pre>";
记得。如果返回成功,你又看不到效果,可以将公众号先取消关注,再加为关注,立刻就能看到效果了。
否则,你要等24小时左右 才能看到效果。
顺便,我也可以承接微信开发(请不要咨询我微信开发怎么做,网上教程很多。。。)
ltc监控脚本

ltc监控脚本

最近BTC和LTC很火啊。。但我又没有时间关注它,所以就写了个脚本,利用macosx 的notification来提醒。其实有很多办法,比如chrome的桌面提醒等。windows的messmage,邮件都OK

我这个只是最简单的版本,花了5分钟写完。。用了一些技术:
1、php
2、crontab
3、terminal-notifier (这个是ruby的,在mac下面安装也很简单,直接brew install terminal-notifier 就OK了)
上代码:
if (count($argv) == 1) {
echo "\nUse age:\n\n";
echo "php okcoin.php --ltc=100 --btc=500 \n\n";
echo "--ltc 代表参数,ltc和btc不分顺序\n\n";
exit;
}
array_shift($argv);
$params = array();
foreach ($argv as $val) {
if (strncasecmp($val, "--", 2) === 0) {
$p = explode("=", substr($val, 2));
if (!isset($p[1])) {
continue;
}
$params[trim($p[0])] = trim($p[1]);
}
}
if(!$params){
exit("参数不正确");
}
$api = "https://www.okcoin.com/ticker.do?random=" . rand(1, 9);
$data = @json_decode(file_get_contents($api), true);

$warning = array(
'btc' => 5000,
'ltc' => 150,
);

if ($data) { //证明是正常数据
$btcLast = $data['btcLast'];
$ltcLast = $data['ltcLast'];
if (isset($params['btc']) && $btcLast <= $params['btc']) {
exec("terminal-notifier -message '关注一下BTC,价格在指定价位附近了' -title 'BTC 警告'");
}
if (isset($params['ltc']) && $ltcLast <= $params['ltc']) {
exec("terminal-notifier -message '关注一下LTC,价格在指定价位附近了' -title 'LTC 警告'");
}
}
exit;
自用,只是够用就好
Magento 中为FORM添加自定义的判断

Magento 中为FORM添加自定义的判断

搜索了一些网上的教程,发现网上对于自定义的判断,都是说加个字段或者基于prototype来进行判断就可以了。什么require-text代表必填 之类的。这段时间在对magento做处理的时候发现这样的判断好痛苦(好象,我是指基于产品的FORM的扩展),如果是自己做二次开发,自己写表单,当然这样就OK了

翻看了一下程序,我开始看到资料就是指require-text之类的扩展,那我想,我只要能够加入一个class属性,我就可以基于这个class属性进行判断了,不是吗?

于是我找到了模版的layout,重新定义了layout,表示我可以引入一个指定的js文件,或者引入了代码。

然后,我找到了dispatchEvent,发现有一个事件,但我看了下,在这个事件里我几乎没有什么事情可以做【不过,基于这个事件,我把SKU,日期等做成了自动生成,只是他对表单的验证并没有什么帮助】

找了半天,最后发现数据库有一个backend_model,原来可以为这些程序指定事件,发现SKU有一个事情,是判断 sku的长度不能超过N个字符的,于是对着这个model进行了测试,发现果然OK。添加了几个model,然后update一下数据库,一切就完成了

Magento 中 后台 tag save的时候的BUG

Magento 中 后台 tag save的时候的BUG

magento的后台中在tag进行保存的时候做了一次处理,它原先的代码是:

if (isset($postData['tag_assigned_products']) ) {

意思是如果form提交的时候,有tag_assigned_products字段的话,会进行处理。然而,如果你当时的tag是inactive的时候,在tag of administrator的列表 中是看不到有tag的。这个时候你点保存。那么原来那些tagof administrator的tag就会全部被删除 了。怎么办?

只要你点击那个状态条,看到列表的时候,这个字段就有值了。这时候只有两种处理方式:

1、刷新页面重做处理

2、修改程序。

所以我改成了:

if (isset($postData['tag_assigned_products']) && $postData['tag_assigned_products']) {

OK,收工

Add tags when creating product in Magento?

Add tags when creating product in Magento?

A) to add tags directly when entering/editing a product, you will need an extension, such as this one. This method is most useful to let people with less magento experience create tags (such as data entry people).

B) To add tags without leaving the administrator (not going to the frontend). This is useful for applying the same tag to multiple products:

Create tags through Catalog->All Tags-> Add New Tag button.

After creating the tags, click on each tag. You’ll see a detailed set of options to “Edit Tag”.

Go to the “Products tagged as administrator” tab, click on ‘Reset Filter” . You see all your products getting listed.

Select the products to which you want to assign the tag & click on “Save Tag”.

C) To add tags via the frontend & the backend, which is useful when you want to add a lot of tags to each single product:

Just create your products as normal. When you are ready to add tags to your products just create yourself a customer account for your store and go to each product and add them as a customer would. I wrote out all of my tags in a text editor. Now since I am using phrases, and not just words, my tags will be entered as:

‘Stylus C64’ ‘Stylus C66’ ‘Stylus C84’ ‘Stylus C84N’ ‘Stylus C84WN’ ‘Stylus C86’ ‘Stylus CX3600’ ‘Stylus CX3650’ ‘Stylus CX4600’ ‘Stylus CX6400’ ‘Stylus CX6600’

Take note that you need the single quote (’) to bracket your phrases… or just spaces between each word if your tags are single words. Very easy to write out and then copy/paste into the Add Your Tags section of each product.

Once you copy/paste your tags in the front-end you will be prompted with a message that they must be approved from the admin/moderator. Now log into the back-end and go to the tags section (Catalog > Tags > Pending Tags). There you will see the list of tags you just submitted from the front-end of the site. Now just click “Select All”, then from the Actions drop-down menu choose “Change Status”, and then from the status drop-down choose “Approved” and then “Submit”.

Now you can pick a method most appropriate for each type of use!

http://stackoverflow.com/questions/9261935/add-tags-when-creating-product-in-magento

magento 中处理 catalog_product_prepare_save 行为

magento 中处理 catalog_product_prepare_save 行为

最近在做magento二次开发,遇到个问题,即,我要针对商品的数据中的某些数据做二次处理。看了一下代码,发现在initProduct前有一个event事件:catalog_product_prepare_save,所以我想我只要覆盖这个方法就OK了。网上找了些资料,发现如果再绑定一个event则多个Event都会执行。于是,我就按照这个思路在config.xml里加入了events事件。

然后在Model下面加了Observer.php来监听事件。为了测试系统自有的event能够工作,我在系统默认的event里做了一下输出,发现确实有变化 了。所以证明我的思路还是正确的。当然,这也借鉴了网上的一些资料

config.xml里加入:

<adminhtml>
<events>
<catalog_product_prepare_save>
<observers>
<xxx_web_observer>
<type>singleton</type>
<class>Xxx_Web_Model_Observer</class>
<method>prepareProductAuthor</method>
</xxx_web_observer>
</observers>
</catalog_product_prepare_save>
</events>
</adminhtml>

本来我class想写成xxxx/observer的,但死活通不过,我不知道是我写错了。还是。。最后写成详细的类名才OK。(也可能是因为不在系统目录下,而在local目录下的原因吧?)

Observer就不写了。参考core/mage/bundle/model/observer.php即可(本来有大小写的,我忽略了)

php能做什么?

php能做什么?

最近好象关于PHP能做什么的话题又出来了。其实说白了,PHP啥都能做,进可攻退可守。有人说,PHP没有多线程,没有这没有那。其实很多人都用PHP实现了这些功能。

先说其他的,上次thinkinlamp聚会的时候,一位台湾朋友就展示了php用来做类似单片机功能的事情。也从侧面证明了,PHP可以搞类似驱动的事情(也只能在linux下面,毕竟linux下面任何东西都是文件)
PHP还能做什么?有人用php来自己做Web服务器。或许有人说了,PHP自带不是有PHP -S xxx:81吗?其实是很多人用socket自己实现了webserver。
还有人说PHP没有多线程,但PHP在linux下面fork进程后,也能算是当成多线程了吧。
只是目前可能是异步在实现上没有那么好吧。
有些项目大家可以看看的,先不谈有人直接用C实现PHP的框架,这没意思,为了提升性能,这些代码我们都看不到了。
不过,有些纯用PHP实现的玩意可以学习一下。这些代码,也不错,思路更不错
1、reactphp
2、aopphp
3、国内的swolephp
这些都是改变思路的程序,如果你要学多进程,其实,你可以看APNSPHP,即能用来当成apns server,你也可以照着学多进程的程序。
BAE的坑

BAE的坑

自从GAE出来后。什么SAE,ACE,BAE什么什么的就全部出来了

ACE我跑几个程序一直有问题,禁用了一些函数,所以我最近在看BAE。速度也不错,也能绑域名。所以它就成了首选。但BAE有一个坑 需要注意
如果你的程序的上传没有改过。那么一般都是上传在自己当前的项目目录里,而这个目录由于没有经过svn上传,所以它并不在版本库里,因此,一旦碰到BAE来一个svn迁移的事件。或者更新的时候,极有可能你上传的那些附件会全部丢失。
慎重。我就觉得BAE就象一个实验室。我们上传代码到BAE的svn库里。然后svn库会自动 checkout到BAE的平台。于是。。如果他们要迁移了。这些数据应该都是直接delete。然后再checkout一份。
建议找个程序,上传到BAE自带的存储中(就象sae的存储)。
转:2011-10-12 PHP重用curl句柄, CURLOPT_HTTPGET的BUG

转:2011-10-12 PHP重用curl句柄, CURLOPT_HTTPGET的BUG

转的文章。主要是因为经常会用curl来抓取数据。而且几乎是http_get和http_post混用。但我没遇上这个BUG。不清楚以后会不会遇上,所以我先记录一下,以防万一遇到时候不知道怎么做。

原文地址来自:http://www.ideawu.net/blog/archives/622.html

重用一个CURL句柄时, 发现curl_setopt($ch, CURLOPT_HTTPGET, TRUE) 不起作用. 期望在调用这条语句之后发起请求, 应该发送的是GET, 但看服务器log, 却使用了和前一次请求相同的HTTP方法.

PHP脚本:

<?php $url = 'http://www.ideawu.net/';  $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_exec($ch);  curl_setopt($ch, CURLOPT_HTTPGET, true); // 错误! BUG curl_exec($ch);  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // 正确 curl_exec($ch); ?>

web server log:

124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net 124.127.130.50 "2011-10-12 18:55:09" "POST / HTTP/1.1" 200 3516 "-" www.ideawu.net 124.127.130.50 "2011-10-12 18:55:09" "GET / HTTP/1.1" 200 3516 "-" www.ideawu.net

这个BUG目前还没找到相关的资料.

补充: 不仅仅是CURLOPT_HTTPGET, CURLOPT_POST也有同样的问题. 所以, 结论是: 只有CURLOPT_CUSTOMREQUEST才是正确的方法.

开始要关注性能了

开始要关注性能了

不得不说,我现在要开始关注性能和稳定性了

最近检查命令行下程序的输出,发现最后退出的LOG大多数是allow memory ....的问题,固然这个命令行下面内存释放有问题有关,但主动释放还是必要的。
毕竟web页在处理完当前请求后,还能够被回收掉一点。php-fpm之类的,还能帮忙,但命令行,它就没有断开过,一直在运行,因此,需要注意一下释放了
或者在无法释放的时候,自己设置一个临界点。到临界点的时候自我重启一下也OK。
逐步开始研究下