方维二次开发

wangtongphp

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

将方维采集到的商品详情里面的链接转换成淘宝客链接的方法:

方维系统,采集到的详情,会添加到goods表,content字段;

share.service.php 474行左右; 在这里可以处理写入数据库的内容,注意转义字符;

        //add by wangtongphp QQ:1006440989 ,获取商品id转化为淘客url
        preg_match_all("/href=.\".[^\"]*(\?|\&)id=(\d{1,})/",$goods['content'],$out);
        if(!empty($out[2])){
            foreach($out[2] as $k=>$v){
                //$goods['content'] = 'id:'.$v.'--'.$goods['content'];
                $wt_taoke_url = FS('Goods')->getTaobaokeURLById($v);
                if(!empty($wt_taoke_url)){
                    //$goods['content'] = 'url:'.$wt_taoke_url.'--'.$goods['content'];
                    $wt_to_url = FU('tgo',array('url'=>$wt_taoke_url,'uid'=>$current_obj['uid'],'sid'=>$current_obj['share_id'],'gid'=>$current_obj['goods_id']));
                    $goods['content'] = str_replace($out[0][$k],'href=\"'.$wt_to_url,$goods['content']);
                }
            }
        }

 

 

在goods.service.php 比对collect函数,写一个通过商品ID转换淘宝客链接的函数;

/**
     * 淘宝URL转化 ,将商品ID转化为淘客URL
     * @author  add by wangtongphp, QQ:1006440989,提供有偿服务
     * @param int $id
     * @return url
     */
    public function getTaobaokeURLById($num_iid)
    {
        setTimeLimit(3600);
        global $_FANWE;
        include_once FANWE_ROOT.'sdks/taobao/TopClient.php';
        include_once FANWE_ROOT.'sdks/taobao/request/TaobaokeItemsDetailGetRequest.php';
        Cache::getInstance()->loadCache('business');
        
        $client = new TopClient;
        $client->appkey = trim($_FANWE['cache']['business']['taobao']['app_key']);
        $client->secretKey = trim($_FANWE['cache']['business']['taobao']['app_secret']);
        
        $req = new TaobaokeItemsDetailGetRequest;
        $req->setFields("num_iid,click_url");
        $req->setNumIids($num_iid);
        $req->setPid($_FANWE['cache']['business']['taobao']['tk_pid']);
        $resp = $client->execute($req);
        if(isset($resp->taobaoke_item_details) && isset($resp->taobaoke_item_details->taobaoke_item_detail))
        {
            $taobaoke_item_details = (array)$resp->taobaoke_item_details;
            $taobaoke_item_detail = (array)$taobaoke_item_details['taobaoke_item_detail'];
             return $taobaoke_item_detail['click_url'];
        }else{
            return false;
        }
    }

 

note.module.php还可以对数据库里面的数据在输出之前进行处理

 

//add by wangtongphp 获取商品id转化为淘客url,转载请注明来源:http://www.cnblogs.com/wangtongphp/archive/2013/05/29/3105212.html

 

        //add by wangtongphp 获取商品id转化为淘客url ,QQ:1006440989 ,
        preg_match_all("/href=\".[^\"]*(\?|\&)id=(\d{1,})/",$current_obj['desc'],$out);
        if(!empty($out[2])){
            foreach($out[2] as $k=>$v){
                $wt_taoke_url = FS('Goods')->getTaobaokeURLById($v);
                //echo $wt_taoke_url;
                if(!empty($wt_taoke_url)){
                    $wt_to_url = FU('tgo',array('url'=>$wt_taoke_url,'uid'=>$current_obj['uid'],'sid'=>$current_obj['share_id'],'gid'=>$current_obj['goods_id']));
                    $current_obj['desc'] = str_replace($out[0][$k],"href=\"".$wt_to_url,$current_obj['desc']);
                }
                
            }
        }
        $current_obj['desc'] = preg_replace("/href=\"[^\/tgo].[^\"]*/","href=\"".$current_obj['to_url'],$current_obj['desc']);

 

将方维采集到的淘宝天猫商品详情里面的链接转换成淘宝客链接,这样就可以了

posted on 2013-05-29 00:13  方维二次开发  阅读(1942)  评论(1编辑  收藏  举报