京东联盟开发(9)——同步第三方优惠券平台

 

 

   //定时 - 同步逛丢信息  
    public function cron_sync_guangdiu(){
        $index = 0;
        Vendor('Html.simple_html_dom');
        $goods = M("guang_goods");

        $category = array('stockup'=>'囤货','baby'=>'母婴','daily'=>'日用','digital'=>'数码','electrical'=>'家电','food'=>'食品','clothes'=>'美妆配饰','sport'=>'运动户外','automobile'=>'汽车用品');
        $key = array_rand($category,1);
        $value = $category[$key];

        $url = "http://guangdiu.com/cate.php?k=".$key."&m=%E4%BA%AC%E4%B8%9C%E5%95%86%E5%9F%8E";
        var_dump($url);
        try
        {
            $headers = array(
                'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                // 'Accept-Encoding: gzip, deflate, sdch, br',
                'Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            $output = curl_exec($ch);
            curl_close($ch);

            // echo $output;
            // exit;
            $html_main = str_get_html($output);
            // $html_main = file_get_html($url);
            var_dump($html_main);

            if (!isset($html_main) || !$html_main){
                // $html_main->clear();
                return "文件载入出错!";
            }

            foreach(@$html_main->find('div[class="gooditem withborder"]') as $iteminfo)
            {
                $gdurl = @$iteminfo->find('div[class="rightlinks"] a', 0)->href;
                $gdid = str_replace("go.php?id=","",$gdurl);

                if (empty($gdid)){
                    continue;
                }

                $gd2url = "http://guangdiu.com/go.php?id=".$gdid;
                $ch2 = curl_init();
                curl_setopt($ch2, CURLOPT_URL, $gd2url);
                // curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
                $output2 = curl_exec($ch2);
                curl_close($ch2);

                preg_match('/item\.jd\.com%2F(\d+).html/i', $output2, $match);
                if (!isset($match[1]) || empty($match[1])){
                    continue;
                }

                $skuid = $match[1];
                $gdtitle = trim(@$iteminfo->find('a[class="goodname"]', 0)->plaintext);
                $gddescritpion_x = str_replace("完整阅读>", "",trim(@$iteminfo->find('a[class="abstractcontent"]', 0)->plaintext));
                $gddescritpion = str_replace(array('完整阅读>', '    ', '  ', ' ', '某东'), array("", ' ', ' ', '', '京东'), $gddescritpion_x);
                $gdtimestamp = time();

                $data = array();
                $data = array('gdid'            => $gdid,
                              'skuid'           => $skuid,
                              'gdtitle'         => $gdtitle,
                              'gddescritpion'   => $gddescritpion,
                              'gdcategory'      => $value,
                              'gdtimestamp'     => $gdtimestamp
                );
                // var_dump($data);
                $isadd = $goods->add($data);
                $index ++;
            }
            $html_main->clear();
            $this->success('同步条目'.$index, 'guang');
        }catch (Exception $e){
            return $e;
        }
    }

 

posted @ 2019-07-11 22:29 方倍工作室 阅读(...) 评论(...) 编辑 收藏